如何让一组切换按钮像WPF中的单选按钮一样?

cod*_*oop 117 wpf

我有一组按钮,它们应该像切换按钮一样,但也可以作为单选按钮,在当前时间只能选择/按下一个按钮.它还需要具有不选择/按下任何按钮的状态.

行为将有点像Photoshop工具栏,其中零或一个工具随时被选中!

知道如何在WPF中实现这一点吗?

小智 290

在我看来,这是最简单的方法.

<RadioButton Style="{StaticResource {x:Type ToggleButton}}" />
Run Code Online (Sandbox Code Playgroud)

请享用! - Pricksaw

  • 应该选择这个答案.为您提供无线电按钮的所有好处,没有绑定到某些控制器的缺点.我首先尝试绑定以分离不可见的radiobutton集,但这有不必要的开销,最终出现了一个错误,其中所有单击的按钮看起来都突出显示但未必检查. (29认同)
  • 或者,如果您需要将其应用于元素内的所有RadioButtons(例如`Grid`),请使用`<Grid.Resources> <Style TargetType ="RadioButton"BasedOn ="{StaticResource {x:Type ToggleButton}}"/ > </Grid.Resources>`. (15认同)
  • 此方法的一个缺点是您无法通过单击选中的单选按钮取消选中单选按钮. (14认同)
  • 可以用这种togglebutton的自定义样式吗? (2认同)
  • @wondra您只能为FrameworkElement分配一个样式.但是,您可以调整romkyns解决方案并继承ToggleButton样式:````<Style BasedOn ="{StaticResource {x:Type ToggleButton}}"x:Key ="Blubb"TargetType ="RadioButton"> <Setter Property = "背景"值="黄色"/> </ Style>```` (2认同)
  • 对于WinRT/UWP:`{x:Type ...}`不会在这里工作.但您可以将[here](https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj709930.aspx?f=255&MSPPError=-2147217396)的默认ToggleButton样式复制到ResourceDictionary和使用`<Style BasedOn ="{StaticResource DefaultToggleButtonStyle}".. />`代替 (2认同)
  • @Julien 如果您想“通过单击选中的按钮来取消选中(按钮)”,则不应使用“RadioButton”。重点是: RadioButton [“代表一个可以被用户选择但不能清除的按钮”](https://msdn.microsoft.com/en-us/library/system.windows.controls.radiobutton. aspx)。如果您想允许用户取消选中它,请使用“ToggleButton”。 (2认同)

Bry*_*son 39

最简单的方法是设置ListBox的样式,以便为其ItemTemplate使用ToggleButtons

<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>
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用ListBox的SelectionMode属性来处理SingleSelect与MultiSelect.

  • 这是一个糟糕的例子,因为你混淆了显示和行为.您可以通过datatemplate更改显示,但仍然具有列表框的行为,而**不是*一组无线电/切换按钮的行为.键盘交互很奇怪.一个更好的解决方案是带有RadioButtons的ItemsControl,样式为ToggleButtons(参见其他最高投票答案). (4认同)

RoK*_*oKK 31

<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>
Run Code Online (Sandbox Code Playgroud)

它适合我,享受!

  • 这是一个很好的解决方案.但是,当我们有一个事件.它将被调用两次. (2认同)

Sam*_*Sam 5

你总是可以在ToggleButton的Click上使用一个泛型事件,它在VisualTreeHelper的帮助下将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;
    }
Run Code Online (Sandbox Code Playgroud)