【发布时间】:2013-05-06 01:31:42
【问题描述】:
我想根据 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}}" />
如何实现这一目标?
【问题讨论】:
-
媒体和艺术家都是类?在枚举中,它们只是字符串。甚至是字典<string,IEnumerable>因为 IEnumerable 需要一个类型,所以不会起作用。
-
是的,它们是类。
标签: wpf binding datagrid listbox