组合框将 itemsource 绑定到自定义列表并将 selecteditem 绑定到该列表的实例不起作用 [英] Combobox binding itemsource to custom list and selecteditem to an instance of that list doesn't work

查看:21
本文介绍了组合框将 itemsource 绑定到自定义列表并将 selecteditem 绑定到该列表的实例不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了非常不同的方法来让我的组合框工作,但我仍然卡住了:(

I tried really different ways to make my combobox working but I'm still stuck :(

这是我的应用程序的一个非常简化的版本:(刚刚编辑,如有错误请见谅)

Here is a very simplified version of my app : (just edited, sorry for mistakes)

<ListView ItemsSource="{Binding People}" SelectedItem="{Binding SelectedPerson}"/>
<ComboBox ItemsSource="{Binding Grades}" SelectedItem="{Binding SelectedPerson.MyGrade}" 
     DisplayMemberPath="Name"/>

以及背后的代码:

public class Person
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            if (name != value)
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }

    private Grade myGrade;
    public Grade MyGrade
    {
        get { return myGrade; }
        set
        {
            if (myGrade != value)
            {
                myGrade = value;
                NotifyPropertyChanged("MyGrade");
            }
        }

    }

    //-- INotifyPropertyChanged implementation
}
public class Grade
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            if (name != value)
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }

    private int prop;
    public int Prop
    {
        get { return prop; }
        set
        {
            if (prop != value)
            {
                prop = value;
                NotifyPropertyChanged("Prop");
            }
        }

    }

    //-- INotifyPropertyChanged implementation
}
public partial class MainWindow : Window
{
    public ObservableCollection<Person> People { get; set; }
    public ObservableCollection<Grade> Grades { get; set; }

    private Person selectedPerson;
    public Person SelectedPerson
    {
        get { return selectedPerson; }
        set
        {
            if (selectedPerson != value)
            {
                selectedPerson = value;
                NotifyPropertyChanged("SelectedPerson");
            }
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        People = new ObservableCollection<Person>();
        Grades = new ObservableCollection<Grade>();

        Grades.Add(new Grade() { Name = "Grade 1", Prop = 1 });
        Grades.Add(new Grade() { Name = "Grade 2", Prop = 2 });

        People.Add(new Person() { Name = "guy 1", MyGrade = Grades[0] });
        People.Add(new Person() { Name = "guy 2", MyGrade = Grades[0] });
        People.Add(new Person() { Name = "guy 3", MyGrade = Grades[1] });
    }

    //-- INotifyPropertyChanged implementation
}

问题是当我在列表视图中选择一个项目时,组合框仍然是空的.itemsource 没问题(如果我点击组合框,我可以看到1 级"和2 级").我认为有一些东西要告诉Person.GradeGrades 列表的一部分",但我找不到什么.

The problem is that the combobox is still empty when I select an item in the listview. The itemsource is OK (If I click on the combobox, I can see "grade 1" and "grade 2"). I think there is something missing to tell "the Person.Grade is part of the Grades list" but I cannot find what.

希望你能帮助我;)

推荐答案

SelectedItem 在内存中是否与 ItemsSource 中的项目完全相同?

Is the SelectedItem the exact same reference in memory as the item in the ItemsSource?

默认情况下,WPF 将通过引用将 SelectedItemItemsSource 中的项目进行比较,如果它们在内存中不是相同的引用,它将返回 no匹配项.

By default, WPF will compare the SelectedItem to the items in the ItemsSource by reference, and if they're not the same reference in memory, it will return no matching item.

如果您无法在代码中执行此操作,最常见的解决方法是:

If you are unable to do this in your code, the most common workarounds are to either:

  • SelectedValue绑定到值类型而不是引用类型,并设置SelectedValuePath

  • Bind the SelectedValue to a value type instead of a reference type, and set the SelectedValuePath

<ComboBox ItemsSource="{Binding Grades}" 
          SelectedValue="{Binding SelectedPerson.MyGrade.GradeId}" 
          SelectedValuePath="GradeId"
          DisplayMemberPath="Name"/>

  • 或者重写 .Equals() 以确保两个对象在特定属性匹配时被认为是相等的,而不是在内存中的引用匹配时被认为是相等的.

  • Or override the .Equals() to ensure the two objects are considered equal when specific properties match, as opposed to being considered equal when the reference in memory matches.

    public override bool Equals(object obj) 
    { 
        if (obj == null || !(obj is Grade)) 
            return false; 
    
        return ((Grade)obj).GradeId == this.GradeId); 
    }
    

  • 这篇关于组合框将 itemsource 绑定到自定义列表并将 selecteditem 绑定到该列表的实例不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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