Closing the View sets properties of the ViewModel

2019-05-11 07:31发布

问题:

I use a DataGrid to display a list of Animals in my WPF application:

The value for ComboBox "Bucht" is loaded from another collection Pens in my ViewModel using the following XAML, which works fine:

<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, e.g. by clicking on another View, some of the properties of my ViewModel are set to null. If I reopen the View the Pens are set to null which looks like that:

The debugger confirms it:

I think this is related to the question WPF View sets ViewModel properties to null on closing. But I can't use the workaround provided in those answers (i.e. setting UpdateSourceTrigger=LostFocus in my ComboBox) because I save the entity directly after edit, so LostFocus update is to late.

Is there any clean way to avoid that behaviour?


回答1:

The problem seems to be the ItemsSource binding. When you navigate to other view, the ItemsSource binding returns null, items from combobox are removed, SelectedItem is set to null and SelectedItem.Binding updates updates Pen property.

You can try one of following:

  1. Try to use OneTime binding in the ItemsSource property, so it is not cleared.
  2. ViewModel's responsibility is to allow view to bind easily. Add Pens property next to Pen property. Never mind that it will be the same foreach animal. It will be just reference to the same collection, so no performance or consistency problems.

One more comment. Remove the SelectedValuePath and SelectedValue Binding. First of all, they conflicts with SelectedItem, second, you used them incorrectly - SelectedValuePath="PenId" would make sense, if you had PenId property instead of Pen property.



回答2:

Change the UpdateSourceTrigger on the SelectedItem binding to Default



回答3:

change the itemssource binding and try once. I mean instead of binding from Relative source datacontext, try to bind from viewModel and check once.



标签: c# wpf mvvm