0
votes

I have a DataGrid. Let it have two columns name and Gender :

<DataGrid............>
    <DataGridTemplateColumn Header="Name">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Name, RelativeSource.........}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="Age">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ComboBox SelectedItem="{Binding Selected, RelativeSource.........}" >
                    <sys:String> Male </sys:String>
                    <sys:String> Female </sys:String>
                </ComboBox>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid>

Now When I run the program, and suppose I have 5 rows in the DataGrid, thenwhen I select male in any row e.g. 3rd row, then all the combobox's SelectedItem becomes Male. How can I avoid this?

When my program runs I have only 1 row, But when I add more rows it looks like :

enter image description here

When I try to select Female in any ComboBox :

enter image description here

When I select Female in any ComboBox :

enter image description here

I know that my Binding is Incorrect, so I am facing such a problem, but can anybody explain me the ComboBox's SelectedItem Binding inside a DataGrid?

1
DataGrid is a Control.Vishal
Could you show the entire definition of SelectedItem binding? And what is the ItemsSource of the DataGrid?vadim
Selected property resides where?Rohit Vats
@RohitVats Selected Property resides in GroupsViewModel.Vishal
So, you are binding all rows with same property. You should bind with the property in underlying source if you want it to be set individually for all rows.Rohit Vats

1 Answers

0
votes

Since you are trying to bind List and you have ComboBoxItem objects as items. Set your ComboBox SelectedValuePath="Content" like this

 <ComboBox SelectedValuePath="Content" ....... >
                    <sys:String> Male </sys:String>
                    <sys:String> Female </sys:String>
  </ComboBox>