【问题标题】:Binding a listView to display data associated with it in another listView绑定 listView 以在另一个 listView 中显示与其关联的数据
【发布时间】:2021-02-01 19:42:35
【问题描述】:

尊敬的 StackOverflow 用户。我被分配了一个任务:我有一个从 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#,所以请原谅我的愚蠢问题。如果有人能提供帮助,我将不胜感激

【问题讨论】:

  • 您好,欢迎您。在 MainWindow() 中,foreach 将添加学生列表视图中所有组的所有学生。你可能想关闭那部分。也许只是添加第一组的学生。您需要为 GroupsView 列表框实现 .SelectedValueChanged 方法。在该方法中,清除当前学生列表,并用所选组中的学生填充 StudentsView。
  • 您好!感谢你的回复!很抱歉,我无法支持您的建议
  • 没问题。我没有写一个完全编码的答案,所以没关系。

标签: c# wpf xaml data-binding


【解决方案1】:
  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}” … />

【讨论】:

  • 弗兰克,非常感谢!我要试一试!我迷路了,现在我明白了方向
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多