我从这开始: 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中不可能有任何解决方法吗?
感谢
答案 0 :(得分: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;
}
}
}
当你运行它时,你会看到一个仅包含值&#34; RelativeOrAbsolute&#34;的组合。和&#34;绝对&#34; (&#34;相对&#34;被过滤掉)。