【发布时间】:2012-12-29 15:09:47
【问题描述】:
我有以下项目数据类,以及一个转换器。
class ListBoxViewItem
{
public String Name { get; set; }
public Boolean IsChecked { get; set; }
}
[ValueConversion(typeof(List<String>),typeof(List<ListBoxViewItem>))]
class ListToItemConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return null;
IEnumerable<String> l = value as IEnumerable<String>;
return (from n in l select new ListBoxViewItem() { IsChecked = true, Name = n });
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
class ListBoxData
{
public List<String> AllData
{
get
{
return new List<string>()
{
"FOO",
"BAR"
};
}
set
{
}
}
}
我将ListBoxData 的实例绑定到列表框控件的ItemsSource。如下:
<ListBox>
<ListBox.ItemsSource>
<Binding>
<Binding.Path>AllData</Binding.Path>
<Binding.Converter>
<local:ListToItemConverter />
</Binding.Converter>
<Binding.Mode>TwoWay</Binding.Mode>
</Binding>
</ListBox.ItemsSource>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay}"
Content="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
所以我的问题是,在显示列表框时会调用Convert 函数,但是由于此列表框中的每个项目都是一个复选框,虽然我使用TwoWay 绑定来绑定实例和列表框,但@987654328当我选中/取消选中此列表框中的复选框时,不会调用 @。
我不确定ConvertBack 是否可以按我的预期工作。但是如果我希望ConvertBack可以在检查状态更改时触发,我该怎么办?
【问题讨论】:
-
不,我知道何时为文本框控件调用 ConvertBack,但就我而言,我想知道何时为 itemscontrol 中更改的项属性之一调用 ConvertBack。
标签: c# wpf xaml itemscontrol updatesourcetrigger