如何让一组切换按钮充当 WPF 中的单选按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2362641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a group of toggle buttons to act like radio buttons in WPF?
提问by code-zoop
I have a group of buttons that should act like toggle buttons, but also as radio buttons where only one button can be selected / pressed down at a current time. It also need to have a state where none of the buttons are selected / pressed down.
我有一组按钮应该像切换按钮一样,但也可以作为单选按钮,在当前时间只能选择/按下一个按钮。它还需要有一个没有任何按钮被选择/按下的状态。
The behavior will be kind of like Photoshop toolbar, where zero or one of the tools are selected at any time!
该行为将有点像 Photoshop 工具栏,其中可以随时选择零个或一个工具!
Any idea how this can be implemented in WPF?
知道如何在 WPF 中实现吗?
采纳答案by Bryan Anderson
The easiest way is to style a ListBox to use ToggleButtons for its ItemTemplate
最简单的方法是为 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>
Then you can use the SelectionMode property of the ListBox to handle SingleSelect vs MultiSelect.
然后您可以使用 ListBox 的 SelectionMode 属性来处理 SingleSelect 与 MultiSelect。
回答by Uday Kiran Thummalapalli
This is easiest way in my opinion.
这是我认为最简单的方法。
<RadioButton Style="{StaticResource {x:Type ToggleButton}}" />
Enjoy! -- Pricksaw
享受!-- 电锯
回答by RoKK
<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>
it works for me, enjoy!
它对我有用,享受!
回答by Sam
you could always use a generic event on the Click of the ToggleButton that sets all ToggleButton.IsChecked in a groupcontrol(Grid, WrapPanel, ...) to false with the help of the VisualTreeHelper; then re-check the sender. Or something in the likes of that.
在 VisualTreeHelper 的帮助下,您始终可以在 ToggleButton 的 Click 上使用通用事件,该事件将 groupcontrol(Grid, WrapPanel, ...) 中的所有 ToggleButton.IsChecked 设置为 false;然后重新检查发件人。或者类似的东西。
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;
}
回答by Arsen Mkrtchyan
you can put grid with radiobuttons in it, and create button like template for raduiobuttons. than just programmaticaly remove check if you don't want buttons to be toggled
您可以将带有单选按钮的网格放入其中,并为单选按钮创建类似模板的按钮。不只是以编程方式删除检查如果您不想切换按钮
回答by Jojo Sardez
You can also try System.Windows.Controls.Primitives.ToggleButton
你也可以试试 System.Windows.Controls.Primitives.ToggleButton
<ToggleButton Name="btnTest" VerticalAlignment="Top">Test</ToggleButton>
Then write code against the IsChecked
property to mimick the radiobutton effect
然后针对该IsChecked
属性编写代码以模拟单选按钮效果
private void btnTest_Checked(object sender, RoutedEventArgs e)
{
btn2.IsChecked = false;
btn3.IsChecked = false;
}
回答by Simon F
I did this for RibbonToggleButtons, but maybe it's the same for regular ToggleButtons.
我是为 RibbonToggleButtons 这样做的,但也许对于常规的 ToggleButtons 也是如此。
I bound the IsChecked for each button to a "mode" enum value using EnumToBooleanConverter from here How to bind RadioButtons to an enum?(Specify the enum value for this button using the ConverterParameter. You should have one enum value for each button)
我从这里使用 EnumToBooleanConverter 将每个按钮的 IsChecked 绑定到“模式”枚举值如何将 RadioButtons 绑定到枚举?(使用 ConverterParameter 指定此按钮的枚举值。每个按钮应该有一个枚举值)
Then to prevent unchecking a button that's already checked, put this in your code behind for the Click event for each of the RibbonToggleButtons:
然后,为了防止取消选中已选中的按钮,请将其放在每个 RibbonToggleButton 的 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;
}
}
}
回答by Prince Owen
To help people like Julian and me (two minutes ago...). You can derive from the RadioButton
like this.
帮助像朱利安和我这样的人(两分钟前......)。你可以从中得出RadioButton
这样的结果。
class RadioToggleButton : RadioButton
{
protected override void OnToggle()
{
if (IsChecked == true) IsChecked = IsThreeState ? (bool?)null : (bool?)false;
else IsChecked = IsChecked.HasValue;
}
}
Then, you can use it like Uday Kiran suggested...
然后,你可以像 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>
This method allows only one ToggleButton
to be Checked
at a time, and it also allows UnChecking.
这种方法只允许一个ToggleButton
是Checked
在一个时间,并且它也允许取消选中。
回答by Strawberryshrub
I took a few piece of the answers and added some extra code. Now you can have different groups of toggle buttons which act like one toggle button:
我拿了一些答案并添加了一些额外的代码。现在,您可以拥有不同组的切换按钮,它们的作用类似于一个切换按钮:
<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>
And the different groups of radio buttons which look like toggle buttons:
以及看起来像切换按钮的不同组的单选按钮:
<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}}">