2

我从这个开始: 按位标志枚举 和这个: 访问枚举成员

这是我的代码

[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 中不可能,你有什么解决方法吗?

谢谢

4

1 回答 1

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”被过滤掉了)。

于 2012-04-30T18:19:18.120 回答