关闭视图会将 ViewModel 的属性设置为 null [英] Closing the View sets properties of the ViewModel to null

查看:34
本文介绍了关闭视图会将 ViewModel 的属性设置为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 DataGrid 在我的 WPF 应用程序中显示 Animals 列表:

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

ComboBox "Bucht" 的值是使用以下 XAML 从我的 ViewModel 中的另一个集合 Pens 加载的,效果很好:

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>

<小时>

现在的问题:如果视图关闭,例如通过单击另一个视图,我的 ViewModel 的某些属性设置为 null.如果我重新打开视图,笔将设置为 null,如下所示:


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:

调试器确认:

我认为这与 WPF View 在关闭时将 ViewModel 属性设置为 null 的问题有关.但是我不能使用这些答案中提供的解决方法(即在我的 ComboBox 中设置 UpdateSourceTrigger=LostFocus),因为我在编辑后直接保存实体,所以 LostFocus 更新是迟到了.

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.

有什么干净的方法可以避免这种行为吗?

推荐答案

问题似乎是 ItemsSource 绑定.当您导航到其他视图时,ItemsSource 绑定返回 null,组合框中的项目被删除,SelectedItem 设置为 null,SelectedItem.Binding 更新更新 Pen 属性.

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. 尝试在 ItemsSource 属性中使用 OneTime 绑定,这样它就不会被清除.
  2. ViewModel 的职责是允许视图轻松绑定.在 Pen 属性旁边添加 Pens 属性.没关系,它会是相同的 foreach 动物.它将只是对同一个集合的引用,因此不会出现性能或一致性问题.
  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.

还有一条评论.移除 SelectedValuePathSelectedValue 绑定.首先,它们与 SelectedItem 冲突,其次,您错误地使用了它们 - SelectedValuePath="PenId" 是有道理的,如果您拥有 PenId 属性而不是 Pen 属性.

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.

这篇关于关闭视图会将 ViewModel 的属性设置为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆