Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF将组合框所选项目绑定到listview当前项目_C#_Wpf_Listview_Combobox_Bind - Fatal编程技术网

C# WPF将组合框所选项目绑定到listview当前项目

C# WPF将组合框所选项目绑定到listview当前项目,c#,wpf,listview,combobox,bind,C#,Wpf,Listview,Combobox,Bind,我是WPF的新手,所以我肯定错过了一些非常明显的东西 我有一个简单的设置: public partial class MainWindow : Window { public ObservableCollection<Student> Students { get; set; } public ObservableCollection<Student> ClassroomStudents { get; set; } public MainWin

我是WPF的新手,所以我肯定错过了一些非常明显的东西

我有一个简单的设置:

 public partial class MainWindow : Window
{
    public ObservableCollection<Student> Students { get; set; }
    public ObservableCollection<Student> ClassroomStudents { get; set; }

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

        Students = new ObservableCollection<Student>();

        Students.Add(new Student { ID = 1, Name = "John" });
        Students.Add(new Student { ID = 2, Name = "Paul" });
        Students.Add(new Student { ID = 3, Name = "Ringo"});
        Students.Add(new Student { ID = 4, Name = "George" });

        ClassroomStudents = new ObservableCollection<Student>();
        ClassroomStudents.Add(Students[0]); //View binds it ok at startup (one way)
        ClassroomStudents.Add(Students[1]); //View binds ut ok at startup (one way)

    }


    public class Student : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public int ID { get; set; }
        public string Name { get; set; }
    }

    private void bOK_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(ClassroomStudents[0].Name);

}
我发现一些答案建议绑定到属性(ie ID),但我想绑定到集合项(Student)

谢谢,
Rodo.

我能想到的最简单的修复方法是将
ClassroomStudents
项目包装在一个选择容器中:

public class StudentContainer : INotifyPropertyChanged
{
    public Student Item { get; set; } // TODO: INotifyPropertyChanged implementation
}
更改集合和初始化:

public ObservableCollection<StudentContainer> ClassroomStudents { get; set; }
// ...
ClassroomStudents.Add(new StudentContainer { Item = Students[0] });
public observetecollection ClassroomStudents{get;set;}
// ...
Add(newstudentcontainer{Item=Students[0]});
更改XAML

<ComboBox                                    
    ItemsSource="{Binding Students, RelativeSource={ RelativeSource AncestorType=Window}}"
    DisplayMemberPath="Name"
    SelectedItem="{Binding Path=Item,Mode=TwoWay}"
    >


我希望这足以给你一个起点。您的代码中存在一些与您的问题没有直接关系的次要问题,但可能需要注意才能使整个问题正常进行。

创建一个
ChosenStudent
类型的属性
Student
,该属性将包含所选项目。然后像这样使用它
SelectedItem=“{Binding Path=DataContext.ChosenStudent,Mode=TwoWay,RelativeSource={RelativeSource FindAncestor,antestortype={x:Type ListView}}}}”
“模型上的集合值没有改变”——为什么要这样做?更改组合框中的选择不会重新排列集合的顺序,那么为什么
ClassroomStudents[0]
会返回与以前不同的
Student
对象呢?甚至不算您的
组合框
控件似乎绑定到
学生
集合,而不是
教室学生
集合。坦率地说,您完全不清楚您期望或希望发生什么,也不清楚为什么,特别是考虑到您显然已经排除了将
SelectedItem
属性绑定到的单独属性(根据前面的注释),您不能双向绑定当前DataContext,只能绑定上下文中的属性。您的绑定应该会出现警告或错误
{binding Path=,Mode=TwoWay}
这是完美的,正是我所需要的。非常感谢。
public ObservableCollection<StudentContainer> ClassroomStudents { get; set; }
// ...
ClassroomStudents.Add(new StudentContainer { Item = Students[0] });
<ComboBox                                    
    ItemsSource="{Binding Students, RelativeSource={ RelativeSource AncestorType=Window}}"
    DisplayMemberPath="Name"
    SelectedItem="{Binding Path=Item,Mode=TwoWay}"
    >