结合WPF组合框枚举和隐藏某些值 [英] Binding WPF ComboBox to enum and hiding certain values

查看:215
本文介绍了结合WPF组合框枚举和隐藏某些值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到一个枚举这样一个WPF组合框:

I have a WPF combobox that is bound to an enum like this:

<Window.Resources>
    <local:EnumDescriptionConverter x:Key="enumDescriptionConverter"/>
    <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="cityNamesDataProvider">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:MyModel+CityNamesEnum"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

<ComboBox x:Name="cityNameComboBox" ItemsSource="{Binding Source={StaticResource cityNamesDataProvider}}" SelectionChanged="cityNameComboBox_SelectionChanged">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource enumDescriptionConverter}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

这是我结合枚举具有属性的描述,看起来像这样:

The enum that I'm binding to has description attributes and looks like this:

public enum CityNamesEnum
{
    [Description("New York City")]
    NewYorkCity,
    [Description("Chicago")]
    Chicago,
    [Description("Los Angeles")]
    LosAngeles
}

我并不总是要显示的每个枚举值。是否有可能触发一个或多个枚举值的知名度?如果这些ComboBoxItems,我想我可以简单地将.Visibility属性设置为隐藏,但因为他们是枚举值,我不知道这是可能的。有谁知道?

I don't always want to display each enum value. Is it possible to toggle the visibility of one or more of the enum values? If these were ComboBoxItems, I think I could simply set the .Visibility property to hidden but since they're enum values, I'm not sure if this is possible. Does anyone know?

推荐答案

为什么不创建一个不过滤你一个普通的C#方法,然后有ObjectDataProvider的指向该方法,而不是

Why not just create a normal C# method which does the filtering for you and then have the ObjectDataProvider point to that method instead

static method IEnumerable<CityNamesEnum> MyFilter() {
  yield return CityNames.NewYorkCity;
  yield return CityNames.Chicago;
}

XAML

<ObjectDataProvider 
   MethodName="MyFilter" 
   ObjectType="{x:Type local:TheType}" 
   x:Key="cityNamesDataProvider">
</ObjectDataProvider>

这篇关于结合WPF组合框枚举和隐藏某些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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