根据另一个列表框的选择填充 WPF 列表框

Populate WPF listbox based on selection of another listbox(根据另一个列表框的选择填充 WPF 列表框)
本文介绍了根据另一个列表框的选择填充 WPF 列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到 observablecollection 的列表框.observable 集合包含一个对象列表,每个对象都有自己的 observablecollection.我想要的是单击第一个列表框中的一个项目,并将它的列表显示在第二个列表框中.我可以在纯 WPF 中执行此操作吗?

I have a listbox that is bound to an observablecollection. The observable collection contains a list of objects, each with it's own observablecollection. What i want is to click an item in the first listbox and have it's list of things displayed in the second listbox. Can I do this in pure WPF?

推荐答案

只需将第二个列表框的ItemsSource绑定到第一个列表框的SelectedItem即可.

Just bind the ItemsSource of the second listbox to the SelectedItem of the first list box.

这里有一些代码.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        TestItems = new ObservableCollection<Test>();
        InitializeComponent();

        for (int i = 0; i < 5; i++)
            TestItems.Add(InitTest(i));
    }

    public ObservableCollection<Test> TestItems { get; set; }

    private Test InitTest(int index)
    {
        Test test = new Test();
        test.Name  = "Test" + index.ToString();
        test.Test2Items =  new ObservableCollection<Test2>();

        for (int i = 0; i <= index; i++)
        {
            Test2 test2 = new Test2();
            test2.Label = test.Name + "_label" + i.ToString();
            test.Test2Items.Add(test2);
        }
        return test;
    }
}

public class Test
{
    public string Name { get; set; }
    public ObservableCollection<Test2> Test2Items { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

public class Test2
{
    public string Label { get; set; }
    public override string ToString()
    {
        return Label;
    }
}

Xaml

 <Window x:Class="WpfApplication1.MainWindow"
        x:Name="MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WPF Example" Height="300" Width="400">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <ListBox x:Name="ListBox1" Grid.Column="0" ItemsSource="{Binding TestItems, ElementName=MyWindow}" />
        <ListBox Grid.Column="1" ItemsSource="{Binding SelectedItem.Test2Items, ElementName=ListBox1}" />
    </Grid>
</Window>

这篇关于根据另一个列表框的选择填充 WPF 列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)