1
votes

I am trying to bind some data to a datagrid using the MVVM pattern with WPF. I have confirmed that the datagrid is populating and indeed, that the specific value (Gender) is populated. I've also tried every fix that I could find online (including other questions on this site) that's why I am seeking out an answer here.

<DataGridTemplateColumn Header="Gender" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding Genders}"  SelectedItem="{Binding Gender, UpdateSourceTrigger=PropertyChanged}" IsSynchronizedWithCurrentItem="True">
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Things I've tried: Mode=TwoWay, UpdateSourceTrigger=PropertyChanged", IsSynchronizedWithCurrentItem="True". Although, I am not a super experienced WPF and MVVM programmer, so it could be something simple that I just don't know about. My models seem to be working elsewhere and they implement observable/are in observable collections where applicable.

Edit: I got it sorted out. Here is the code that worked for my issue (in case someone else has a similar problem).

<DataGridTemplateColumn Header="Gender" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding Path=DataContext.Genders, RelativeSource={RelativeSource FindAncestor, AncestorType = Window}}" SelectedItem="{Binding Gender, UpdateSourceTrigger=PropertyChanged}">
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
1
It seems odd that you've added ComboBoxItem elements but also tried to bind the ItemsSource property. I would remove the ComboBoxItem entries from your XAML and see if that helps. - RogerN
Ah, yes. That's a good point. That's the result of trying a lot of things. It seems like when I remove the ComboBoxItem's the ComboBox is blank. Which might be a symptom of the problem, I think. - J. Schauble
Check that the ItemsSource is binding correctly to your Genders property. Is it located on the same object as the Gender property? Can you set a breakpoint inside the getter to see if it's accessed? - RogerN
Also, check the Output window in Visual Studio while debugging the project. Sometimes binding errors will print warnings there but otherwise fail silently. - RogerN
I put a breakpoint on the getting and it doesn't seem to be getting accessed. My datagrid is bound to a Dependent property of a patient which is in the view model. Genders is just a simple string array that's created in the viewmodel. - J. Schauble

1 Answers

0
votes

Assuming you DataGrid's DataContext is the Patient object, try using a RelativeSource binding to point at the DataGrid:

<DataGridTemplateColumn Header="Gender" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=DataContext.Genders}"
                      SelectedItem="{Binding Gender, UpdateSourceTrigger=PropertyChanged}"
                      IsSynchronizedWithCurrentItem="True">
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>