我有一个绑定到对象集合的属性ComboBox
。Items
我还有SelectedItem
绑定到整个集合的属性,ValueConverter
旨在检查集合中的元素并返回要选择的 1 个项目。这部分有效。
不起作用的是当用户在 ComboBox 上进行选择更改时,不会调用的ConvertBack(...)
方法。ValueConverter
我需要ConvertBack(...)
调用,因为我需要接受用户的选择,重新检查集合,并适当地编辑旧的选定项目和新选定的项目。
我知道这种方法很尴尬,但就是这样。以下是相关代码:
组合框:
<ComboBox ItemsSource="{Binding}" SelectedItem="{Binding Path=., Converter={StaticResource ResourceKey=DataInputAssetChoiceSelectedItemConverter}}" />
值转换器:
public class DataInputAssetChoiceSelectedItemConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
foreach (CustomObject Choice in (Collection<CustomObject>)value)
{
if (Choice.IsSelected)
{
return Choice;
}
}
return ((Collection<CustomObject>)value).First();
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{ //breakpoint...execution never gets here!
return null;
}
}
那么为什么从来没有ConvertBack(...)
被调用呢?这只是我误解的事情ComboBox
吗?SelectedItem
我已经使用, SelectedValue
,尝试过这种方法SelectedIndex
,并尝试过使用UpdateSourceTrigger
各种绑定模式、DataTriggers,但似乎永远无法ConvertBack(...)
被调用。使用SelectionChanged
事件是唯一的选择吗?如果是这样,为什么?