我有一個綁定到observablecollection的列表框。可觀察集合包含一個對象列表,每個對象都有自己的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. 從另一個列表框選擇填充列表框
- 2. 當我選擇另一個列表框時填充列表框
- 3. 基於列表框2選擇Excel VBA填充列表框1
- 4. 基於另一個選擇框中的選擇填充一個選擇框 - jQuery?
- 5. 基於另一個組合框的選擇填充組合框
- 6. MVC 4列表框填充另一個列表框
- 7. 用戶窗體列表框填充和清除,取決於從另一個列表框中的選擇
- 8. 從選擇填充列表框,python
- 9. 填充選擇基於另一個選擇php陣列
- 10. 填充列表框
- 11. 填充列表框
- 12. 填充列表框
- 13. WPF列表框選擇
- 14. WPF列表框:與選擇
- 15. 選擇基於另一個表的值從另一個表列
- 16. 在選擇另一個選擇框時填充選擇框
- 17. 使用jQuery和xml填充基於來自另一個選擇列表的索引的選擇列表
- 18. 填充一個選擇框基於另一個選擇數組時
- 19. 從列表框選擇填充多個文本框
- 20. WP7:根據來自另一個列表框的輸入填充列表框
- 21. 列表框列表框的另一個
- 22. Sharepoint:基於另一列中的選擇填充列|合併列表中的2個列表
- 23. 顏色基於另一個列表框值的一個列表框
- 24. 基於複選框值的項目從一個列表到另一個列表
- 25. WPF水平列表框垂直填充
- 26. 負載列表以用於離改變另一個列表框值 當基於另一個列表框值
- 27. Django的 - jquery:基於另一個組合框的選擇填充組合框
- 28. 基於GXT中另一個下拉列表的選擇填充下拉菜單
- 29. 基於選擇另一個下拉列表的下拉列表
- 30. 基於Combobox選擇過濾列表框
selecteditem是一個對象,它具有裏面有一個可觀察的集合。可觀察的集合是我想綁定到第二個列表框,但我不知道如何。 – ConditionRacer
謝謝!不知道我可以做到這一點... ItemsSource =「{Binding SelectedItem.Test2Items – ConditionRacer