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

时间:2012-06-06 17:38:03

标签: c# wpf listbox

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

2 个答案:

答案 0 :(得分:1)

将第二个列表框的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>

答案 1 :(得分:0)

您的视图模型可能如下所示:(我在这里使用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}"/>