ComboBox(Previous DataContext)SelectedItem属性在UWP中设置为空值吗? [英] ComboBox(Previous DataContext) SelectedItem property set as null value in UWP?

查看:61
本文介绍了ComboBox(Previous DataContext)SelectedItem属性在UWP中设置为空值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初, ComboBox DataContext 设置为 Profession1 SelectedValue 作为政治家。在运行时,我将 Datacontext 更改为 Profession2 。这样做会将 Profession1 更改为null。

Initially, ComboBox DataContext is set with Profession1 and SelectedValue as Politician. At Runtime, i changed the Datacontext to Profession2. Doing this is changing the Profession1 to null.

<Page.Resources>
    <local:MainPageViewModel x:Key="datacontent"></local:MainPageViewModel>
</Page.Resources>

<ComboBox x:Name="comboBox"
    ItemsSource="{Binding Professions,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
    SelectedItem="{Binding Profession, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
    Width="100"
    Height="100"                              
    VerticalAlignment="Center"
    HorizontalAlignment="Stretch" />



隐藏代码:



Code Behind:

var datacontent = (this.Resources["datacontent"] as MainPageViewModel);
this.comboBox.DataContext = datacontent.Profession1;



型号:



Model:

public class MainPageViewModel
{       
    public MainPageViewModel()
    {
        Profession1 = new Person();        
        Profession2 = new Person();
    }
    private Person profession1;

    public Person Profession1
    {
        get { return profession1; }
        set { this.profession1 = value; }
    }

    private Person profession2;

    public Person Profession2
    {
        get { return profession2; }
        set { this.profession2 = value; }
    }
}

public class Person : INotifyPropertyChanged
{       
    public Person()
    {
        _professions = new List<string>();
        _professions.Add("Lawyer");
        _professions.Add("Politician");
        _professions.Add("Other");
    }

    private string _profession;
    public string Profession
    {
        get
        {
            if (string.IsNullOrWhiteSpace(_profession))
            {
                // _profession = _professions.LastOrDefault();
            }
            return _profession;
        }
        set
        {
            if (_profession != value)
            {
                _profession = value;
                NotifyPropertyChanged("Profession");
            }
        }
    }      

    private List<string> _professions;

    public List<string> Professions
    {
        get
        {
            return _professions;
        }
    }
}

我使用了以下代码,以检查先前的数据上下文(Profession1-> Professon)值。

I have used the below code, to check the previous datacontext (Profession1->Professon) value .

代码

((this.Resources["datacontent"] as MainPageViewModel).Profession1 as Person).Profession 

输出为:null。
期望值:政客

Output is : null. Expected value : Politician

请对此提出建议。

推荐答案


((this.Resources [ datacontent]作为MainPageViewModel)。Profession1作为Person)。专业

输出为:null。期望值:政客
请有人对此提出建议。

Output is : null. Expected value : Politician Please someone suggest on this.

问题是当您修改<$ c的DataContext时$ c> combobox ,首先将DataContext设置为null,然后将其变为 Profession2 。因此, Profession1 Profession 属性将设置为null。根据您的要求,您可以设置判断条件来解决此问题。

The problem is that when you modify the DataContext of combobox, the DataContext is set null first and then turns to Profession2. So the Profession property of Profession1 will be set null. For your requirement, you could set the judgment condition to solve this issue.

public string Profession
{
    get
    {

        return _profession;
    }
    set
    {
        if (_profession != value && value != null)
        {
            _profession = value;
            OnPropertyChange();
        }
    }
}

这篇关于ComboBox(Previous DataContext)SelectedItem属性在UWP中设置为空值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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