简体   繁体   English

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

[英]Binding a listView to display data associated with it in another listView

dear StackOverflow users.亲爱的 StackOverflow 用户。 There's a task I'm given: I have a list of students that I get from JSON.我得到了一个任务:我有一个从 JSON 获得的学生名单。

[
    {
        "Name":         "Grant",
        "Groupname":    "Group1"
    },
    {
        "Name":         "Tim",
        "Groupname":    "Group2"
    },
    {
        "Name":         "Spencer",
        "Groupname":    "Group3"
    }
    .....
]

Then I have two listBoxes: first for Groups, second for Students belonging to that group.然后我有两个列表框:第一个用于组,第二个用于属于该组的学生。 The problem I cannot get to solve: displaying students of the currently selected group in listBox using Binding and DataContext.我无法解决的问题:使用 Binding 和 DataContext 在 listBox 中显示当前选定组的学生。 So I need help.所以我需要帮助。 Declarations of Student and Group:学生和团体声明:

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;
    }
}

The code below uses List of groups and each group has a list of students.下面的代码使用组列表,每个组都有一个学生列表。 And at this moment I'm stuck.而此刻我被困住了。

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);
        }
    }
}

The listBoxes in XAML 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>

I know it has something to do with XAML but I suddenly got this problem and need to finish it quickly.我知道它与 XAML 有关,但我突然遇到了这个问题,需要尽快完成。 And last time I used C# in 2017 so please excuse my silly question.上次我在 2017 年使用 C# 所以请原谅我的愚蠢问题。 If anyone could help, I'd be so grateful如果有人可以提供帮助,我将不胜感激

  1. First, you need to implement INotifyPropertyChanged for MainWindow, so you will have OnPropertyChanged();首先,您需要为 MainWindow 实现 INotifyPropertyChanged,因此您将拥有 OnPropertyChanged();
public class MainWindow : INotifyPropertyChanged

See here, https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-implement-property-change-notification?view=netframeworkdesktop-4.8请参阅此处, https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-implement-property-change-notification?view=netframeworkdesktop-4.8

And in the constructor, add: this.DataContext = this;并在构造函数中添加: this.DataContext = this;

  1. Then add following properties:然后添加以下属性:
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. Data binding:数据绑定:
<ListBox ItemsSource=”{Binding Groups}” SelectedItem=”{Binding SelectedGroup}” … />
<ListBox ItemsSource=”{Binding Students}” … />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM