我有一组按钮,应该像切换按钮一样工作,但也像单选按钮一样,当前只有一个按钮可以被选择/按下。还需要一种状态,其中没有任何按钮被选择/按下。
行为类似于 Photoshop 工具栏,在任何时候都只有零个或一个工具被选择!
有任何想法如何在 WPF 中实现这个功能吗?
在我看来,这是最简单的方法。
<RadioButton Style="{StaticResource {x:Type ToggleButton}}" />
享受! -- Pricksaw
最简单的方法是为ListBox设置ToggleButtons的ItemTemplate样式
<Style TargetType="{x:Type ListBox}">
<Setter Property="ListBox.ItemTemplate">
<Setter.Value>
<DataTemplate>
<ToggleButton Content="{Binding}"
IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
然后您可以使用ListBox的SelectionMode属性来处理SingleSelect与MultiSelect。
<RadioButton Content="Point" >
<RadioButton.Template>
<ControlTemplate>
<ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
</ControlTemplate>
</RadioButton.Template>
</RadioButton>
对我来说可行,尽情享受吧!
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
int childAmount = VisualTreeHelper.GetChildrenCount((sender as ToggleButton).Parent);
ToggleButton tb;
for (int i = 0; i < childAmount; i++)
{
tb = null;
tb = VisualTreeHelper.GetChild((sender as ToggleButton).Parent, i) as ToggleButton;
if (tb != null)
tb.IsChecked = false;
}
(sender as ToggleButton).IsChecked = true;
}
您可以在网格中放置单选按钮,并为单选按钮创建类似模板的按钮。然后,如果您不希望按钮被切换,只需以编程方式移除选中状态即可。
System.Windows.Controls.Primitives.ToggleButton
。 <ToggleButton Name="btnTest" VerticalAlignment="Top">Test</ToggleButton>
然后针对 IsChecked
属性编写代码,以模仿单选按钮的效果。
private void btnTest_Checked(object sender, RoutedEventArgs e)
{
btn2.IsChecked = false;
btn3.IsChecked = false;
}
ListBoxItem
内使用。 - ANeves我为RibbonToggleButtons做了这个,但也许对于普通的ToggleButtons也是一样的。
我使用了来自如何将RadioButtons绑定到枚举? 的EnumToBooleanConverter,将每个按钮的IsChecked绑定到一个“模式”枚举值上(使用ConverterParameter为此按钮指定枚举值。每个按钮应该有一个枚举值)
然后,为了防止取消选中已选中的按钮,请在每个RibbonToggleButtons的Click事件的代码后面放置以下代码:
private void PreventUncheckRibbonToggleButtonOnClick ( object sender, RoutedEventArgs e ) {
// Prevent unchecking a checked toggle button - so that one always remains checked
// Cancel the click if you hit an already-checked button
var button = (RibbonToggleButton)sender;
if( button.IsChecked != null ) { // Not sure why checked can be null but that's fine, ignore it
bool notChecked = ( ! (bool)button.IsChecked );
if( notChecked ){ // I guess this means the click would uncheck it
button.IsChecked = true;
}
}
}
为了帮助像Julian和我这样的人(两分钟前...)。你可以像这样从RadioButton
派生。
class RadioToggleButton : RadioButton
{
protected override void OnToggle()
{
if (IsChecked == true) IsChecked = IsThreeState ? (bool?)null : (bool?)false;
else IsChecked = IsChecked.HasValue;
}
}
然后,您可以像使用Uday Kiran 建议...一样使用它。
<Window x:Class="Sample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Sample"
Title="MainWindow" Height="600" Width="600">
<StackPanel>
<local:RadioToggleButton Content="Button" Style="{StaticResource {x:Type ToggleButton}}" />
</StackPanel>
</Window>
这种方法只允许一个ToggleButton
被选中,同时也允许取消选中。
<UserControl.Resources>
<Style x:Key="GroupToggleStyle" TargetType="ToggleButton">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding GroupName, RelativeSource={RelativeSource Self}}" Value="Group1"/>
<Condition Binding="{Binding BooleanProperty}" Value="true"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsChecked" Value="true"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
还有不同组的单选按钮,看起来像切换按钮:
<Radio Button GroupName="Group1" Style="{StaticResource {x:Type ToggleButton}}">
<Radio Button GroupName="Group1" Style="{StaticResource {x:Type ToggleButton}}">
<Radio Button GroupName="Group2" Style="{StaticResource {x:Type ToggleButton}}">
<Radio Button GroupName="Group3" Style="{StaticResource {x:Type ToggleButton}}">
ToggleButton _CurrentlyCheckedButton;
然后将一个单一的Checked事件处理程序分配给所有上下文ToggleButton:
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
if (_CurrentlyCheckedButton != null)
_CurrentlyCheckedButton.IsChecked = false;
_CurrentlyCheckedButton = (sender as ToggleButton);
}
还有,一个单独的未经检查的事件处理程序:
private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
if (_CurrentlyCheckedButton == (sender as ToggleButton))
_CurrentlyCheckedButton = null;
}
通过这种方式,您可以拥有所需的“零或一”选择。
Grid
),请使用<Grid.Resources> <Style TargetType="RadioButton" BasedOn="{StaticResource {x:Type ToggleButton}}" /> </Grid.Resources>
。 - Roman Starkov<Style BasedOn="{StaticResource {x:Type ToggleButton}}" x:Key="Blubb" TargetType="RadioButton"><Setter Property="Background" Value="Yellow" /></Style>
- SuigiRadioButton
。这是整个问题的关键: 单选按钮 "代表用户可以选择但不能清除的按钮"。如果你想允许用户取消选中,则应该使用ToggleButton
。 - Disillusioned