我正在尝试使用CheckBoxes获取List,但是在XAML中将Dictionary的值绑定到IsChecked
属性时遇到了一些问题。
我的列表模板:
XAML
<ItemsControl Name="lb" ItemsSource="{Binding Movies}" BorderThickness="0">
<ItemsControl.Style>
<Style TargetType="ItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.Style>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<CheckBox Content="{Binding Key.MovieId}" IsChecked="{Binding Converter={StaticResource DictConvert}, ConverterParameter=Key}" Command="{Binding DummyCommand}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
转换器
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
KeyValuePair<Movie, bool> bla = (KeyValuePair<Movie, bool>)value;
var dict = value as Dictionary<Movie, bool>;
List<KeyValuePair<Movie,bool>> list = new List<KeyValuePair<Movie, bool>>();
list.Add(bla);
dict = list.ToDictionary(k => k.Key, k => k.Value);
if (dict != null)
{
return true;
}
throw new NotImplementedException();
}
调试器点击这个“返回true”行,所以它应该工作,但我得到XamlParseException
。任何人都可以帮我这个吗?我试图创建ObservableDictionary但放弃了 - 这是唯一的方法吗?
答案 0 :(得分:1)
我不认为您可以将ConverterParameter
设置为数据绑定值,就像您尝试的那样(语法也不正确)。将您的转换器重做为IMultiValueConverter
,并将Key和Value作为单独的绑定传递,您应该很高兴。
此外,请删除代码中的throw new NotImplementedException()
行 - 这会使其崩溃。