2012-06-06 2 views
0

observablecollection에 바인딩 된 목록 상자가 있습니다. observable 컬렉션에는 객체 목록이 포함되어 있으며, 각 객체에는 자체 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

선택한 항목은 다음과 같은 개체입니다. 내부에 관찰 가능한 수집 물. 관찰 할 수있는 컬렉션은 내가 두 번째 목록 상자에 바인딩하려는 있지만 방법을 잘 모릅니다. – 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}"/> 
관련 문제