Reputation: 2153
I'm Currently Binding my Datagrid to a DTO. It's able to pull up values EXCEPT the enumeration. How Do I bind enumerations when the values coming in don't quite equal the enumeration.
public enum Channels { Phone, Website, Email, Skype, Cell, Fax }
But the possible values for Channel
are Between 0-5
<DataGrid ItemsSource={Binding ContactMethods} >
<DataGridComboBoxColumn
Header="Type"
SelectedItemBinding="{Binding Channel, Mode=TwoWay}"
shell:EnumHelper.Enum="{x:Type clients:ContactMethods+Channels}"
DisplayMemberPath="Channel"/>
Upvotes: 0
Views: 129
Reputation: 9394
To get all values of an enum
in XAML you can use an ObjectDataProvider
like:
<ObjectDataProvider x:Key="MyEnumDataProvider" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="NameSpaceOfMyEnum:MyEnum"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
To display the enum
-values for example in a ComboBox
(that's where I use this) you have to:
ItemsSource="{Binding Source={StaticResource MyEnumDataProvider}}"
I've created a new Window and added an ObjectDataProvider
in the Window.Resources. The xaml is:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:wpfApplication1="clr-namespace:WpfApplication1"
Title="MainWindow" Height="100" Width="250">
<Window.Resources>
<ObjectDataProvider x:Key="MyEnumDataProvider" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="wpfApplication1:MyEnum"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<ComboBox ItemsSource="{Binding Source={StaticResource MyEnumDataProvider}}"></ComboBox>
</Window>
The enum
is just
enum MyEnum
{
EnumValue1,
EnumValue2,
EnumValue3,
}
I think your xaml from the comment works if you add
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:wpfApplication1="clr-namespace:WpfApplication1"
to your Namespaces of the window. The wpfApplication1 must by modified to match the namespace of your enum
Upvotes: 2
Reputation: 691
public enum Channels { Phone, Website, Email, Skype, Cell, Fax }
var ChannelsForBind = Enum.GetValues(typeof(Channels)).Cast<Channels>();
Upvotes: 0