【问题标题】:ComboBox(Previous DataContext) SelectedItem property set as null value in UWP?ComboBox(Previous DataContext) SelectedItem 属性在 UWP 中设置为空值?
【发布时间】:2018-02-17 03:22:50
【问题描述】:

最初,ComboBox DataContext 设置为 Profession1SelectedValue 作为政治家。在运行时,我将Datacontext 更改为Profession2。这样做会将Profession1 更改为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" />

代码隐藏:

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

型号:

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;
        }
    }
}

我使用下面的代码来检查之前的 datacontext (Profession1->Professon) 值。

代码

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

输出为:空。 期望值:政治家

请有人对此提出建议。

【问题讨论】:

    标签: c# combobox uwp


    【解决方案1】:

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

    输出为:空。期望值:政治家 请有人就此提出建议。

    问题是当你修改combobox的DataContext时,DataContext先设置为null,然后转为Profession2。所以Profession1Profession 属性将被设置为空。根据您的要求,您可以设置判断条件来解决这个问题。

    public string Profession
    {
        get
        {
    
            return _profession;
        }
        set
        {
            if (_profession != value && value != null)
            {
                _profession = value;
                OnPropertyChange();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-30
      • 2011-06-24
      • 2019-02-18
      • 2016-06-15
      • 2011-08-16
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 2017-04-30
      相关资源
      最近更新 更多