我想根据 ListBox.SelectedItem 值设置 DataGrid.ItemsSource 属性,以便每当在 ListBox 中选择一个类别时,DataGrid 应该显示适当的集合。
例子 :
ListBox 有以下项目:
- 艺术家
- 媒体
(取自此枚举)
public enum Category
{
Artists,
Medias
}
然后 DataGrid 应该从这里选择相关的集合:
public class MyCategories
{
public IEnumerable<Media> Medias { get; set; }
public IEnumerable<Artist> Artists { get; set; }
}
但是,DataGrid.ItemsSource 的 ConverterParameter 不允许为其定义绑定。
不能在“Binding”类型的“ConverterParameter”属性上设置“Binding”。只能在 DependencyObject 的 DependencyProperty 上设置“绑定”。
<DataGrid ItemsSource="{Binding Mode=OneWay,
Converter={StaticResource categoriesConverter},
ConverterParameter={Binding ElementName=listBox1, Path=SelectedItem}}" />
如何实现这一目标?