根据所选其他列表框填充 WPF 列表框

本文关键字:列表 WPF 填充 其他 | 更新日期: 2023-09-27 18:36:22

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

根据所选其他列表框填充 WPF 列表框

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

编辑:这是一些代码。

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

哈姆勒

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

您的视图模型可能如下所示: (我在这里使用我的 BindableBase)

class MainViewModel : Bindablebase {
    public ObservableCollection<ItemViewModel> Items { get; private set; }
    private ItemViewModel _selectedItem;
    public ItemViewModel SelectedItem {
        get { return _selectedItem; }
        set { SetProperty(ref _selectedItem, value, "SelectedItem"); }
    }
}
class ItemViewModel : BindableBase {
    public ItemViewModel (string name) {
        Name = name;
        Items = new ObservableCollection<string>();
    }
    public string Name { get; private set; }
    public ObservableCollection<string> Values { get; private set; }
    private string _selectedValue;
    public string SelectedValue {
        get { return _selectedValue; }
        set { SetProperty(ref _selectedValue, value, "SelectedValue"); }
    }
}

然后,您的视图将具有:

<ComboBox ItemsSource="{Binding Items}" 
          SelectedItem="{Binding SelectedItem}"
          DisplayMemberPath="Name"/>
<!-- 
Note that the DataContext here could be ommitted
and the bindings would be like {Binding SelectedItem.Values}
-->
<ComboBox DataContext="{Binding SelectedItem}" 
          ItemsSource="{Binding Values}" 
          SelectedItem="{Binding SelectedValue}"/>