【问题标题】:Manage friendly enum with XAML MarkupExtension使用 XAML MarkupExtension 管理友好的枚举
【发布时间】:2012-05-01 02:19:18
【问题描述】:

我从这里开始: bitwise flag enum 和这个: accessing enum members

这是我的代码

[Flags]
public enum DeliveryDays
{
    [Description("None")]
    NONE = 0, // 0000000

    [Description("monday")]
    MON = 1,  // 0000001

    [Description("tuersady")]
    TUE = 2,  // 0000010

    [Description("wedsnay")]
    WED = 4,  // 0000100

    [Description("thursday")]
    THU = 8,  // 0001000

    [Description("friday")]
    FRI = 16, // 0010000

    [Description("saturday")]
    SAT = 32, // 0100000

    [Description("sunday")]
    SUN = 64, // 1000000

    WORKDAYS = MON | TUE | WED | THU | FRI,  //0011111
    WEEKENDS = SAT | SUN  //1100000
} 

public class Finder 
{
    public DeliveryDays AvailableDays = DeliveryDays.WEEKENDS;

    public DeliveryDays SelectedDays { get; set; }

    public Finder()
    {
        SelectedDays = DeliveryDays.NONE;
    }    
}

这是 XAML

<ComboBox Height="50"
          ItemsSource="{Binding Source={my:Enumeration {x:Type my:DeliveryDays}}}"
          DisplayMemberPath="Description"
          SelectedValue="{Binding SelectedDays}"
          SelectedValuePath="Value" />

现在组合框显示DeliveryDays 类型中的所有枚举,但我只想要AvailableDays 实例成员字段中的枚举值。所以我需要修改自定义MarkupExtension 以仅在我的AvailableDays 字段中显示枚举值。如何从我的自定义 EnumListExtension 方法访问此字段?如果在 XAML 中不可行,您有什么解决方法吗?

谢谢

【问题讨论】:

    标签: wpf xaml binding


    【解决方案1】:

    我不知道纯 XAML 解决方案,但这可行:

    我在UriKind 枚举上测试了它(为了速度)。我认为仅查看整个解决方案会更容易。

    这是 XAML

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            xmlns:sysSys="clr-namespace:System;assembly=System"
            xmlns:cm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
            Title="MainWindow"
            Height="350"
            Width="525">
        <Window.Resources>
            <ObjectDataProvider x:Key="UriKindProvider"
                                ObjectType="{x:Type sys:Enum}"
                                MethodName="GetValues">
                <ObjectDataProvider.MethodParameters>
                    <x:Type Type="sysSys:UriKind" />
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
            <CollectionViewSource x:Key="EnumCollection"
                                  Source="{StaticResource UriKindProvider}"
                                  Filter="CollectionViewSource_Filter"/>
        </Window.Resources>
        <Grid>
            <ComboBox HorizontalAlignment="Center"
                      VerticalAlignment="Center"
                      ItemsSource="{Binding Source={StaticResource EnumCollection}}" />
        </Grid>
    </Window>
    

    这是代码隐藏

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
            {
                var valueToCompare = (UriKind)e.Item;
                e.Accepted = IsInAvailableDays(valueToCompare);
            }
    
            private bool IsInAvailableDays(UriKind value)
            {
                //this is for testing only, replace with actual logic to compare to AvailableDays
                if (value.ToString().Contains("Abs"))
                {
                    return true;
                }
                return false;
            }
        }
    }
    

    当你运行它时,你会看到一个只有值“RelativeOrAbsolute”和“Absolute”的组合(“Relative”被过滤掉了)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      相关资源
      最近更新 更多