0

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 show both the Enum values and to bind the enumType field so my XML can be fixed?

Melanie
  • 584
  • 2
  • 11
  • 31
  • There are loads of questions like this on SO already. See here: http://stackoverflow.com/questions/2608300/databinding-an-enum-to-a-combobox-in-wpf-mvvm – Kieren Johnstone Jun 24 '11 at 12:54
  • Yes, there are tons of questions about wpf enums, but almost all of them concern 1) how to bind an ItemsSource property to an enumeration and 2) how to convert TheEnumsName into "The Enum's Name" in the UI. Your question is *very* confusing, as it appears you are asking this type of question, but in fact you're talking about some sort of XML (MySql enumType list? Don't know, don't MySql). I would STRONGLY suggest you [edit your question](http://stackoverflow.com/posts/6467749/edit) to clarify 1) what the heck enumType is and where it comes from and 2) what the heck it looks like. –  Jun 24 '11 at 13:08

1 Answers1

0

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)

H.B.
  • 166,899
  • 29
  • 327
  • 400