首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ComboBox(以前的DataContext) SelectedItem属性在UWP中设置为空值?

ComboBox(以前的DataContext) SelectedItem属性在UWP中设置为空值?
EN

Stack Overflow用户
提问于 2017-09-08 08:50:39
回答 1查看 111关注 0票数 0

最初,唐纳德·特朗普( ComboBox DataContext )以Profession1SelectedValue为政治家。在运行时,我将Datacontext更改为Profession2。这样做就是将Profession1更改为null。

请参阅以下代码:

代码语言:javascript
复制
<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" />

代码背后:

代码语言:javascript
复制
var datacontent = (this.Resources["datacontent"] as MainPageViewModel);
this.comboBox.DataContext = datacontent.Profession1;

型号:

代码语言:javascript
复制
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 1->Professon)值。

代码语言:javascript
复制
((this.Resources["datacontent"] as MainPageViewModel).Profession1 as Person).Profession 

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

请有人建议一下。

EN

回答 1

Stack Overflow用户

发布于 2017-09-11 05:43:53

((this.Resources["datacontent"] as MainPageViewModel).Profession1 as Person).Profession 输出为: null。期望值:政治家请有人提出这个建议。

问题是,当您修改DataContext of combobox时,首先将DataContext设置为null,然后转到Profession2。因此,Profession属性Profession1将被设置为null。根据您的要求,您可以设置解决此问题的判断条件。

代码语言:javascript
复制
public string Profession
{
    get
    {

        return _profession;
    }
    set
    {
        if (_profession != value && value != null)
        {
            _profession = value;
            OnPropertyChange();
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46112424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档