observablecollection에 바인딩 된 목록 상자가 있습니다. observable 컬렉션에는 객체 목록이 포함되어 있으며, 각 객체에는 자체 observablecollection이 있습니다. 내가 원하는 것은 첫 번째 목록 상자에서 항목을 클릭하고 두 번째 목록 상자에 표시된 항목 목록을 갖도록하는 것입니다. 순수 WPF에서이 작업을 수행 할 수 있습니까?다른 목록 상자를 선택하여 WPF 목록 상자 채우기
0
A
답변
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
같은 것을 볼 수 있었다 당신의보기 모델 : (여기 내가 사용하고 내 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}"/>
관련 문제
- 1. WPF : 창로드시 목록 상자 채우기
- 2. WPF 목록 상자 질문
- 3. 채우기 목록 상자
- 4. ASP.NET 채우기 목록 상자 문제
- 5. 비동기 하위 항목으로 목록 상자 채우기, 목록 상자 고정
- 6. WPF 부동 목록 상자
- 7. 다른 목록 상자에서 항목을 선택하여 숨겨진 텍스트 상자/목록 상자 표시
- 8. WPF 목록 상자 바인딩
- 9. 편집 가능한 WPF 목록 상자
- 10. WPF 목록 상자 선택
- 11. WPF : 목록 상자 유지하기
- 12. WPF 목록 상자 그룹화
- 13. WPF 목록 상자 SelectedItems
- 14. 목록 상자 ScrollIntoView 문제 wpf
- 15. wpf DataGrid 대 목록 상자
- 16. WPF 목록 상자 스크롤 다운
- 17. WPF 목록 상자 스크롤 후 오른쪽 클릭시 목록 상자 항목
- 18. 텍스트 상자에서 목록 상자 채우기
- 19. 대리자를 사용하여 목록 상자 채우기
- 20. vba 테이블에서 채우기 목록 상자
- 21. WPF 목록 상자 - 빈 목록 표시 메시지
- 22. 목록 상자 항목을 선택하여 dockpanels 변경하기
- 23. 데이터베이스 값에서 Matlab GUI 목록 상자 채우기
- 24. WPF 목록 상자 선택 색상
- 25. 다른 목록 항목을 기반으로 목록 항목 채우기
- 26. HTML 목록 상자 PHP를 사용하여 다른 목록 상자 업데이트
- 27. 목록 상자를 다른 목록 상자로 이동
- 28. ASP.NET 다시 게시하지 않고 목록 상자 채우기
- 29. 양식을 통해 목록 채우기
- 30. 목록 상자에서 목록 상자 항목을 가져 오는 방법 wpf
선택한 항목은 다음과 같은 개체입니다. 내부에 관찰 가능한 수집 물. 관찰 할 수있는 컬렉션은 내가 두 번째 목록 상자에 바인딩하려는 있지만 방법을 잘 모릅니다. – ConditionRacer
감사합니다. 이 작업을 수행 할 수 있는지 알지 못했습니다 ... ItemsSource = "{Binding SelectedItem.Test2Items – ConditionRacer