【问题标题】:How to bind ToggleButtons to Enum values on ItemsControl?如何将 ToggleButtons 绑定到 ItemsControl 上的枚举值?
【发布时间】:2014-12-19 16:53:44
【问题描述】:

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

我找到了一种解决方案,可以将一堆单独的 CheckBox 绑定到它们对应的枚举值(每个一个)here,但我正在尝试通过 ItemsControl(从 Enum 类型的值)创建 ToggleButtons,所以我每次我向我的枚举类型添加另一个值(以及创建按钮的更短的 XAML 代码)时,都不必记住添加一个 ToggleButton。问题是我无法绑定 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,它返回给定 Enum 类型的值列表。 EnumBooleanConverter 是来自上述链接的值转换器,如果绑定的枚举值等于其 ConverterParameter 并支持从 bool 到枚举值的 ConvertBack,则返回 true。 SelectedMyEnumValue 是按钮要反映和修改的属性。

这对我来说是一个重复的问题(某些属性无法绑定),所以如果你要给我一个完全不同的 ToggleButtons 方法,还请写下如何解决这种绑定问题。它不必永远绑定,我只需要设置一次值(在 XAML 中没有一堆 Style-and-Triggers 类型的代码)。也许另一个标记扩展可以做到这一点?

谢谢!

【问题讨论】:

  • PS:我还尝试了一个将绑定值与属性值进行比较的 MultiBinding,它几乎可以正常工作。正确的按钮一开始是按下的,但是当您单击另一个按钮时,前一个按钮没有上升(即使 MultiBinding 从每个按钮获取值以进行比较并且返回 false - 我已经通过断点检查了这一点)。也许你知道为什么?

标签: data-binding enums itemscontrol togglebutton


【解决方案1】:

我发现我的问题与this one 重复(我被它的标题弄糊涂了,错过了)。非常遗憾。我根据该帖子的答案(使用 ListBox 而不是 ItemsControl 并将 IsChecked 绑定到 IsSelected)为我的特定案例附加了一个解决方案。这里的主要技巧是隐藏所选项目的蓝色背景的样式。

<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>

无论如何,这只能解决我的 2 个问题之一,所以如果您知道如何使 ConverterParameter 等属性的绑定成为可能,请留下答案。谢谢。

【讨论】:

    猜你喜欢
    • 2011-11-30
    • 2018-06-09
    • 2013-10-02
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多