下面是你在找什麼一個簡單的例子(讓你開始)。
首先創建一個包含所有數據的對象,並將其綁定到ComboBox
,使用組合框SelectedItem
填充ListBox
。
代碼:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Categories.Add(new Category { Name = "Animals", Items = new List<string> { "Dog", "Cat", "Horse" } });
Categories.Add(new Category { Name = "Vehicles", Items = new List<string> { "Car", "Truck", "Boat" } });
}
private ObservableCollection<Category> _categories = new ObservableCollection<Category>();
public ObservableCollection<Category> Categories
{
get { return _categories; }
set { _categories = value; }
}
}
public class Category
{
public string Name { get; set; }
public List<string> Items { get; set; }
}
的XAML:
<Window x:Class="WpfApplication10.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Name="UI">
<StackPanel DataContext="{Binding ElementName=UI}">
<ComboBox x:Name="combo" ItemsSource="{Binding Categories}" DisplayMemberPath="Name"/>
<ListBox ItemsSource="{Binding SelectedItem.Items, ElementName=combo}"/>
</StackPanel>
</Window>
結果:
發表您的當前XAML和代碼。 – 2013-03-19 03:14:09