ComboBox Binding to Enum while preserving enumType value
I'm binding a ComboBox within a DataGrid to Enum values. I get the ComboBox to display the correct values but the problem I'm having is that when I try to Save, I get no XML returned if I use the ComboBox. If I take the ComboBox out then my save works fine, XML is returned and the record is stored.
I'm assuming that this has something to do with the fact that I'm not setting the Path property in my ComboBox, however, if I do set the Path property, then my Enum values don't display in the ComboBox.
My Xaml:
<UserControl.Resources>
<ObjectDataProvider x:Key="dataFromEnum"
MethodName="GetValues"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:TypeExtension Type="local:enumTypes" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.Resources>
<DataGrid Grid.Row="3"
Grid.Column="0"
ItemsSource="{Binding Path=StuffList, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Enum Stuff">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource dataFromEnum}}"
SelectedValuePath="ID" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource dataFromEnum}}"
SelectedValuePath="ID" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
My Class:
private enumTypes _enumType = new enumTypes();
public enumTypes enumType
{
get { return _ enumType; }
set
{
_ enumType = value;
NotifyPropertyChanged(p => enumType);
}
}
I use the enumType value in my SQL table and is needed to send/return XML. But because I'm not binding enumType anywhere I think this is why no XML is given?
Any ideas on how to change my ComboBox binding to sho开发者_开发技巧w both the Enum values and to bind the enumType field so my XML can be fixed?
Your CellTemplate
should not be editable, further you need to bind the SelectedItem
, e.g.
<DataGridTemplateColumn Header="Enum Stuff">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ID}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource dataFromEnum}}"
SelectedItem="{Binding ID}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
(Properties are supposed to be in PascalCase by the way i.e. capitalize enumType
)
精彩评论