如何让一组切换按钮充当 WPF 中的单选按钮? [英] How to get a group of toggle buttons to act like radio buttons in WPF?

查看:29
本文介绍了如何让一组切换按钮充当 WPF 中的单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

该行为有点像 Photoshop 工具栏,可以随时选择零个或一个工具!

The behavior will be kind of like Photoshop toolbar, where zero or one of the tools are selected at any time!

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

Any idea how this can be implemented in WPF?

推荐答案

最简单的方法是为 ListBox 设置样式以将 ToggleButtons 用于其 ItemTemplate

The easiest way is to style a ListBox to use ToggleButtons for its 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.

Then you can use the SelectionMode property of the ListBox to handle SingleSelect vs MultiSelect.

这篇关于如何让一组切换按钮充当 WPF 中的单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆