0
votes

I am trying to bind a ComboBox ItemsSource to the TextWrapping enum within the System.Windows namespace. The end result would be a drop down where the user can select which type of text wrapping to apply for a given object within my application. Everything works fine when I bind to a custom enum, but I can't figure out what path/source I need to use to bind to an enum within the System.Windows namespace. How can I access this namespace through data binding?

    <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>

Update: My enum converter just needs the enum class passed in the xaml, which looks like this for custom enums:

            <ComboBox
                ItemsSource="{Binding Path=MyCreatedEnum, Converter={local:MyEnumConverter}}"
                SelectedValuePath="Value"
                DisplayMemberPath="Description"
                SelectedValue="{Binding Path = TextWrapping}"
                      VerticalAlignment="Center"
                      Margin="5"
                      />
1
I think that depends on what your converter need as input. - Alex.Wei
@Alex.Wei Good point, I updated my question. - cjohnson221

1 Answers

0
votes

Found this over the web under this link.

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

Here is the modified code to suite your requirement.

<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();
    }
}