WPF查看设置视图模型的属性设置为null上收 [英] WPF View sets ViewModel properties to null on closing

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

问题描述

我在哪里,我在一个组框显示用户控件的应用程序。要显示控件,我绑定在主窗体的视图模型,它返回一个ViewModel要显示的属性。我有设置的DataTemplates这样的形式自动知道哪些用户控件/视图用来显示每个视图模型。

I have an application where I'm displaying UserControls in a GroupBox. To display the controls, I'm binding to a property in the ViewModel of the main form, which returns a ViewModel to be displayed. I've got DataTemplates set up so that the form automatically knows which UserControl/View to use to display each ViewModel.

当我展示一个不同的用户控件,我把previous控制的视图模型活跃,但视图是由WPF自动丢弃。

When I display a different UserControl, I keep the ViewModel of the previous control active, but the Views are discarded automatically by WPF.

这是我遇到的问题是,当视图关闭,任何双向绑定,在视图模型属性直接设置为null,所以当我显示视图模型重新所有值都只是置空在UI中。

The problem that I'm having is that when the view shuts down, any two way bindings to the properties in the ViewModel are immediately set to null, and so when I display the ViewModel again all of the values are just set to null in the UI.

我想这是因为作为视图关闭了其配置并清除它包含的控件的任何值,并且由于绑定到位,他们传播到视图模型为好。

I assume this is because as part of the View closing down it disposes and clears any values in the controls it contains, and since the bindings are in place they propagate down to the ViewModel as well.

在我的资源的DataTemplates

DataTemplates in my resources

<DataTemplate DataType="{x:Type vm:HomeViewModel}">
    <vw:HomeView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:SettingsViewModel}">
    <vw:SettingsView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:JobListViewModel}">
    <vw:JobListView />
</DataTemplate>

code用来显示用户控件

Code used to display user controls

<GroupBox>
    <ContentControl  Content="{Binding Path=RightPanel}" />
</GroupBox>

这是我的意见之一,我绑定控件的例子:

Example of a control that I'm binding in one of the Views:

    <ComboBox Name="SupervisorDropDown" ItemsSource="{Binding Path=Supervisors}" DisplayMemberPath="sgSupervisor" 
           SelectedValuePath="idSupervisor" SelectedValue="{Binding Path=SelectedSupervisorID}" />

和相关视图模型属性:

public ObservableCollection<SupervisorsEntity> Supervisors
    {
        get
        {
            return supervisors;
        }
    }

public int? SelectedSupervisorID
{
    get
    {
        return selectedSupervisorID;
    }
    set
    {
        selectedSupervisorID = value;
        this.OnPropertyChanged("SelectedSupervisorID");
    }
}

如何停止我的观点在我的ViewModels归零值任何想法?我想,也许我需要设置显示为空的的DataContext它倒闭过,但我不知道如何去与该办法的事情目前正在结合。

Any idea on how to stop my Views nulling the values in my ViewModels? I'm thinking that maybe I need to set the DataContext of the View to null before it closes down, but I'm not sure how to go about that with the way things are currently binding.

推荐答案

UpdateSourceTrigger 明确为引发LostFocus

如果视图被关闭,并将其数据为空,它在视图模型上的数据没有影响。

If the view is closing and sets its data to null, it has no effect on your data in the viewmodel.

<ComboBox Name="SupervisorDropDown" ItemsSource="{Binding Path=Supervisors}" DisplayMemberPath="sgSupervisor" 
SelectedValuePath="idSupervisor" 
SelectedValue="{Binding Path=SelectedSupervisorID, UpdateSourceTrigger=LostFocus}" />

这篇关于WPF查看设置视图模型的属性设置为null上收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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