2012-06-06 57 views
0

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

回答

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

selecteditem是一个对象,它具有里面有一个可观察的集合。可观察的集合是我想绑定到第二个列表框,但我不知道如何。 – ConditionRacer

+0

谢谢!不知道我可以做到这一点... ItemsSource =“{Binding SelectedItem.Test2Items – ConditionRacer

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}"/> 
相关问题