2012-04-25 34 views
2

我从这个开始: bitwise flag enum 这: accessing enum members管理友好枚举与XAML的MarkupExtension

这是我的代码

[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类型的所有枚举,但我只想要enum实例成员字段中的值为AvailableDays。所以我需要修改自定义MarkupExtension以显示我的AvailableDays字段中的枚举值。如何从我的自定义EnumListExtension方法访问此字段?如果在XAML中不可能有任何解决方法?

感谢

回答

0

我不知道一个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”和“绝对”(”相对“被过滤掉)。