繁体   English   中英

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

[英]Populate WPF listbox based on selection of another listbox

我有一个绑定到observablecollection的列表框。 可观察的集合包含一个对象列表,每个对象都有其自己的可观察的集合。 我想要的是单击第一个列表框中的一个项目,并将其列表显示在第二个列表框中。 我可以在纯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;
    }
}

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>

您的视图模型可能看起来像这样:(我在这里使用我的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}"/>

暂无
暂无

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

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