如何实现从字符串到类型位字段的枚举的类型转换器

时间:2012-03-26 19:41:42

标签: wpf xaml bit-fields

我有一个枚举:

C#

[Flags] 
public enum FieldStyle
{

    FS_ShadeWords = 0x00000001,
    FS_WithWords = 0x00000002,
    FS_WithLetters = 0x00000004,
    FS_MMDD_YYMM = 0x00000008,
    FS_T2WordsUnder = 0x00000010,
    FS_T2Style = 0x00000020,
    FS_AT1 = 0x00000040,
    FS_TP1Keying = 0x00000080,
    FS_T1KeyDate = 0x00000100,
    FS_H8Date =  0x00000200,
    FS_AT1Blob = 0x00000400,
    FS_ThickRight = 0x00000800,
    FS_ThickLeft = 0x00001000,
    FS_GreyBase = 0x00002000,
    FS_C017_Keying = 0x00004000,
    FS_CT23Date = 0x00008000,
    FS_TopEdge = 0x00010000,
    FS_DefaultDate = 0x00020000,
}

在一种风格中,我需要设置如下属性:

XAML:

<Setter Property="WhichFieldStyle" Value="FS_DefaultDate,FS_WithWords"/>

不接受XAML。

有人知道如何在XAML ???中设置位字段

1 个答案:

答案 0 :(得分:0)

我真的不知道你的代码出了什么问题,但这个工作正常

<UserControl x:Class="WpfAppCursorTest.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Name="uc">
  <Grid>
    <TextBox Text="{Binding ElementName=uc, Path=WhichFieldStyle}" />
  </Grid>
</UserControl>
public partial class UserControl1 : UserControl
{
  public static readonly DependencyProperty WhichFieldStyleProperty =
    DependencyProperty.Register("WhichFieldStyle", typeof(FieldStyle), typeof(UserControl1), new PropertyMetadata(default(FieldStyle)));

  public FieldStyle WhichFieldStyle {
    get { return (FieldStyle)this.GetValue(WhichFieldStyleProperty); }
    set { this.SetValue(WhichFieldStyleProperty, value); }
  }

  public UserControl1() {
    this.InitializeComponent();
  }
}
<Window x:Class="WpfAppCursorTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WpfAppCursorTest="clr-namespace:WpfAppCursorTest"
        Title="MainWindow"
        Height="350"
        Width="525">
  <Grid>
    <WpfAppCursorTest:UserControl1 WhichFieldStyle="FS_DefaultDate,FS_WithWords,FS_TP1Keying" />
  </Grid>
</Window>

enter image description here

希望这会有所帮助