Data binding 如何将ToggleButtons绑定到ItemsControl上的枚举值?

Data binding 如何将ToggleButtons绑定到ItemsControl上的枚举值?,data-binding,enums,itemscontrol,togglebutton,Data Binding,Enums,Itemscontrol,Togglebutton,我想为枚举值创建一行切换按钮。按钮必须显示MyEnumType属性的当前值(按其状态),并在按下时更改属性的值 我已经找到了一个解决方案,可以将一组单独的复选框绑定到它们相应的枚举值(每个值对应一个),但是我正在尝试通过ItemsControl(从枚举类型的值)创建ToggleButton,这样我就不必记得每次向枚举类型添加另一个值时都添加ToggleButton了(也适用于创建按钮的较短XAML代码)。问题是我无法绑定ConverterParameter。是否有一种干净、正确的方法来执行此操作

我想为枚举值创建一行切换按钮。按钮必须显示MyEnumType属性的当前值(按其状态),并在按下时更改属性的值

我已经找到了一个解决方案,可以将一组单独的复选框绑定到它们相应的枚举值(每个值对应一个),但是我正在尝试通过ItemsControl(从枚举类型的值)创建ToggleButton,这样我就不必记得每次向枚举类型添加另一个值时都添加ToggleButton了(也适用于创建按钮的较短XAML代码)。问题是我无法绑定ConverterParameter。是否有一种干净、正确的方法来执行此操作?还是我做的每件事都错了

这是我现在的代码:

<ItemsControl ItemsSource="{Binding Source={local:EnumValues {x:Type local:MyEnumType}}}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ToggleButton Content="{Binding Converter={StaticResource MyEnumToNiceStringConverter}}" 
                IsChecked="{Binding Source=mySourceObject, Path=SelectedMyEnumValue, Converter={StaticResource EnumBooleanConverter}, ConverterParameter={Binding}}">
            </ToggleButton>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

local:EnumValues
是一个
MarkupExtension
,它返回给定枚举类型的值列表。
EnumBooleanConverter
是来自上述链接的值转换器,如果绑定的枚举值等于其ConverterParameter,则返回true,并支持从布尔值转换回枚举值。
selectedMyNumValue
是按钮要反映和修改的属性

这对我来说是一个重复的问题(某些属性无法绑定),因此,如果您打算为切换按钮提供一种完全不同的方法,请也写下如何解决这种绑定问题。它不必永远绑定,我只需要设置一次值(在XAML中没有一堆样式和触发器之类的代码)


谢谢!

我发现我的问题是的重复(我被标题弄糊涂了,错过了)。对此很抱歉。我根据那篇文章的答案为我的具体案例附加了一个解决方案(使用列表框而不是ItemsControl,绑定被选中)。这里的主要技巧是隐藏所选项目的蓝色背景的样式

<ListBox ItemsSource="{Binding Source={local:EnumValues {x:Type local:MyEnumType}}}"
         SelectedItem="{Binding Source=mySourceObject, Path=SelectedMyEnumValue}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.Resources>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ToggleButton Content="{Binding Converter={StaticResource MyEnumToNiceStringConverter}}" 
                IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}">
            </ToggleButton>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>


无论如何,这只解决了我的两个问题中的一个,因此,如果您知道如何使诸如ConverterParameter之类的属性绑定成为可能,请留下一个答案。谢谢。

PS:我还尝试了一个多重绑定,它将绑定值与属性值进行比较,几乎成功了。正确的按钮起初是关闭的,但当您单击在另一个按钮上,上一个按钮没有启动(尽管多重绑定从每个按钮获取值进行比较,并返回false-我已经通过断点检查了)。也许你知道为什么?