从xaml绑定到System.Windows枚举

时间:2018-06-21 23:33:44

标签: wpf xaml enums

我试图将ComboBox ItemsSource绑定到System.Windows命名空间中的TextWrapping枚举。最终结果将是一个下拉菜单,用户可以在其中选择要在我的应用程序中为给定对象申请的文本换行类型。当我绑定到自定义枚举时,一切工作正常,但是我无法弄清楚绑定到System.Windows命名空间中的枚举所需使用的路径/源。如何通过数据绑定访问此命名空间?

    <DataTemplate 
        DataType="{x:Type MyObjectWrapper}"
        >
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Text Wrapping" VerticalAlignment="Center" Margin="5,5,0,5"/>
            <ComboBox
                ItemsSource="{Binding Source={???}, Converter={local:MyEnumConverter}}"
                SelectedValuePath="Value"
                DisplayMemberPath="Description"
                SelectedValue="{Binding Path = TextWrapping}"
                      VerticalAlignment="Center"
                      Margin="5"
                      />
        </StackPanel>
    </DataTemplate>

更新:我的枚举转换器只需要在xaml中传递的枚举类,对于自定义枚举,它看起来像这样:

            <ComboBox
                ItemsSource="{Binding Path=MyCreatedEnum, Converter={local:MyEnumConverter}}"
                SelectedValuePath="Value"
                DisplayMemberPath="Description"
                SelectedValue="{Binding Path = TextWrapping}"
                      VerticalAlignment="Center"
                      Margin="5"
                      />

1 个答案:

答案 0 :(得分:0)

在此链接下通过网络找到了它。

http://brianlagunas.com/a-better-way-to-data-bind-enums-in-wpf/

这是修改后的代码,可以满足您的要求。

<Grid x:Name="LayoutRoot">
    <Grid.Resources>
        <local:EnumToListConverter x:Key="enumToListConv" />
    </Grid.Resources>
    <ComboBox
            Margin="5"
            VerticalAlignment="Center"
            ItemsSource="{Binding Source={local:EnumBindingSource {x:Type sysWin1:TextWrapping}}, Converter={StaticResource enumToListConv}}"
            SelectedValuePath="Value" />
</Grid>
public class EnumBindingSourceExtension : MarkupExtension
{

    private Type _enumType;
    public Type EnumType
    {
        get { return this._enumType; }
        set
        {
            if (value != this._enumType)
            {
                if (null != value)
                {
                    Type enumType = Nullable.GetUnderlyingType(value) ?? value;
                    if (!enumType.IsEnum)
                        throw new ArgumentException("Type must be for an Enum.");
                }

                this._enumType = value;
            }
        }
    }

    public EnumBindingSourceExtension() { }

    public EnumBindingSourceExtension(Type enumType)
    {
        this.EnumType = enumType;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (null == this._enumType)
            throw new InvalidOperationException("The EnumType must be specified.");

        Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
        return actualEnumType;
    }
}


public class EnumToListConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var items = Enum.GetValues((Type)value);
        return items;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}