下面是你在找什么一个简单的例子(让你开始)。
首先创建一个包含所有数据的对象,并将其绑定到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