绑定一个 listView 以在另一个 listView 中显示与其关联的数据

Binding a listView to display data associated with it in another listView

亲爱的 Whosebug 用户。我有一项任务:我有一份来自 JSON.

的学生名单
[
    {
        "Name":         "Grant",
        "Groupname":    "Group1"
    },
    {
        "Name":         "Tim",
        "Groupname":    "Group2"
    },
    {
        "Name":         "Spencer",
        "Groupname":    "Group3"
    }
    .....
]

然后我有两个列表框:第一个用于组,第二个用于属于该组的学生。 我无法解决的问题:使用 Binding 和 DataContext 在 listBox 中显示当前选定组的学生。所以我需要帮助。学生和团体声明:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Groupname { get; set; }
}

public class Group
{
    public string Name { get; set; }
    public int Count { get; set; } // how many students are in this group

    public List<Student> Students = new List<Student>();

    public Group()
    {
    }

    public Group(string name, int count)
    {
        this.Name = name;
        this.Count = count;
    }
}

下面的代码使用组列表,每个组都有一个学生列表。而此刻我卡住了。

public MainWindow()
{
    List<Group> groups = new List<Group>();
    
    // I excluded json deserialization and Lists initialization code...

    InitializeComponent();

    foreach (var x in groups)
    {
        GroupsView.Items.Add(x.Name);

        foreach (var y in x.Students)
        {
            StudentsView.Items.Add(y);
        }
    }
}

XAML

中的列表框
<ListBox Name="GroupsView" HorizontalAlignment="Left" Height="172" Margin="48,0,0,0" VerticalAlignment="Center" Width="206"/>

<ListBox Name="StudentsView"  HorizontalAlignment="Left" Height="172" Margin="306,0,0,0" VerticalAlignment="Center" Width="364">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <Run Text="{Binding Path=Id}"></Run>
                <Run Text="{Binding Path=Name}"></Run>
            </TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我知道它与XAML有关,但我突然遇到了这个问题,需要尽快完成它。上次我在 2017 年使用 C# 所以请原谅我的愚蠢问题。如果有人能提供帮助,我将不胜感激

  1. 首先,您需要为 MainWindow 实现 INotifyPropertyChanged,因此您将拥有 OnPropertyChanged();
public class MainWindow : INotifyPropertyChanged

看到这里,https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-implement-property-change-notification?view=netframeworkdesktop-4.8

并在构造函数中添加:this.DataContext = this;

  1. 然后添加以下属性:
private List<Group> _groups;

public List<Group> Groups
{
    get { return _groups; }
    set { _groups = value; OnPropertyChanged(); }
}

private Group _selectedGroup;


public Group SelectedGroup
{
    get { return _selectedGroup; }
    set 
    { 
        _selectedGroup = value; 
        OnPropertyChanged();
        if (_selectedGroup != null)
        {
            Students = _selectedGroup.Students;
        }
    }
}

private List<Student> _students;

public List<Student> Students
{
    get { return _students; }
    set { _students = value; OnPropertyChanged(); }
}
  1. 数据绑定:
<ListBox ItemsSource=”{Binding Groups}” SelectedItem=”{Binding SelectedGroup}” … />
<ListBox ItemsSource=”{Binding Students}” … />