WPF - MVVM - 组合框的SelectedItem [英] WPF - MVVM - ComboBox SelectedItem

查看:242
本文介绍了WPF - MVVM - 组合框的SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有视图模型(实施 INotifyPropertyChanged的)的背景和阶级类别其中有一个类型的某个属性时才字符串。我的组合框的SelectedItem被绑定到一个类别的一个实例。当我改变实例的值,的SelectedItem不被更新,组合框不会改变。

I have ViewModel(implemented INotifyPropertyChanged) in the background and class Category which has only one property of type string. My ComboBox SelectedItem is bind to an instance of a Category. When i change the value of instance, SelectedItem is not being updated and Combobox is not changed.

编辑:code

组合框:

<ComboBox x:Name="categoryComboBox" Grid.Column="1"  Grid.Row="3" Grid.ColumnSpan="2" 
          Margin="10" ItemsSource="{Binding Categories}"
          DisplayMemberPath="Name" SelectedValue="{Binding NodeCategory, Mode=TwoWay}"/>

属性:

private Category _NodeCategory;
public Category NodeCategory
{
    get
    {
        return _NodeCategory;
    }
    set
    {
        _NodeCategory = value;
        OnPropertyChanged("NodeCategory");
    }
}

[Serializable]
public class Category : INotifyPropertyChanged
{
    private string _Name;
    [XmlAttribute("Name")]
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
            OnPropertyChanged("Name");
        }
    }

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    [field:NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;
}

和我所想的是:当我设置

and what I am trying is: when I set

NodeCategory = some_list_of_other_objects.Category;

要在组合框选择的项目适当的DisplayMemberPath

推荐答案

在这一行,你要设置的类别 -

The category you are setting in this line -

NodeCategory = some_list_of_other_objects.Category;

和一个present在你的分类收集(的ItemsSource ={结合分类})应该是指同一个对象。如果他们没有,那么的SelectedItem 将无法正常工作。

and one present in your Categories collection(ItemsSource="{Binding Categories}") should be referring to same object. If they are not then SelectedItem won't work.

解决方法1 -

您也可以尝试使用 SelectedValuePath 这样的 -

You can also try to use SelectedValuePath like this -

<ComboBox x:Name="categoryComboBox" 
          ItemsSource="{Binding Categories}"
          DisplayMemberPath="Name" 
          SelectedValuePath="Name" 
          SelectedValue="{Binding NodeCategory, Mode=TwoWay}" />

在code,你可以做这样的事情 -

and in code you can do something like this -

private string _NodeCategory;
public string NodeCategory
{
    get
    {
        return _NodeCategory;
    }
    set
    {
        _NodeCategory = value;
        OnPropertyChanged("NodeCategory");
    }
}

和这样的一套选择的项目 -

and set selected item like this -

NodeCategory = some_list_of_other_objects.Category.Name;

和这样的使用选择的值 -

and use selected value like this -

Category selectedCategory = 
   some_list_of_other_objects.FirstOrDefault(cat=> cat.Name == NodeCategory);

Category selectedCategory = 
   Categories.FirstOrDefault(cat=> cat.Name == NodeCategory);

解决方案2 -

另一种可能的解决方案可以是 -

Another possible solution can be -

NodeCategory = 
  Categories.FirstOrDefault(cat=> cat.Name == some_list_of_other_objects.Category.Name);

这样你NodeCategory属性将有一个对象的分类收集的参考和的SelectedItem 将工作。

这篇关于WPF - MVVM - 组合框的SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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