Closing the View properties sets the ViewModel properties to null
I am using DataGrid
to display a list of Animals in my WPF application:
The value for ComboBox
"Bucht" is loaded from another Pens collection in my ViewModel using the following XAML, which works great:
<DataGrid ItemsSource="{Binding Path=Animals, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectionMode="Single" AutoGenerateColumns="False" CanUserSortColumns="True">
<DataGrid.Columns>
<DataGridTextColumn Header="EPC" Binding="{Binding Epc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Visual ID" Binding="{Binding VisualId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Geschlecht" Binding="{Binding Gender, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTemplateColumn Header="Bucht">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext.Pens, RelativeSource={RelativeSource AncestorType={x:Type view:AdministrationView}}}"
DisplayMemberPath="Name"
SelectedItem="{Binding Pen, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding Pen.PenId}"
SelectedValuePath="PenId">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding DataContext.SaveCommand, RelativeSource={RelativeSource AncestorType={x:Type view:AdministrationView}}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Now the problem : if the view is closed, eg. clicking on another view some properties of my ViewModel are null. If I open the Pen View again, it is set to null, which looks like this:
The debugger confirms this:
I think it has something to do with the WPF issue View sets ViewModel properties to null on close . But I cannot use the workaround provided in these answers (i.e. Setting UpdateSourceTrigger=LostFocus
in mine ComboBox
) because I am saving the object directly after editing, so the LostFocus update is at the end.
Is there any clean way to avoid this behavior?
source to share
The problem is in the binding of the ItemsSource. When you navigate to another view, the ItemsSource binding returns null, items are removed from the combobox, the SelectedItem is null, and the SelectedItem.Binding updates the Pen properties.
You can try this:
- Try to use OneTime binding on ItemsSource property so it doesn't get cleared.
- It is the responsibility of the ViewModel to allow simple binding. Add a Pens property next to the Pen parameter . It doesn't matter that it will be the same animal. It will just reference the same collection, so there won't be any performance or consistency issues.
One more comment. Remove bindings SelectedValuePath
and SelectedValue
. First, they conflict with SelectedItem, and second, you used them incorrectly - SelectedValuePath = "PenId" makes sense if you have a PenId property instead of a Pen property.
source to share