出于某种原因,IMultiValueConverter
未通过ConvertBack
功能设置我的媒体资源。
我的自定义ComboBox
XAML
如下所示:
<ComboBox.SelectedIndex>
<MultiBinding Converter="{StaticResource IDToIndex}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnTargetUpdated="True">
<Binding Path="SelectedID" ElementName="InnerComboBoxName"/>
<Binding Path="ItemsSource" ElementName="InnerComboBoxName"/>
</MultiBinding>
</ComboBox.SelectedIndex>
和IMultiValueConverter
是这样的:
public class IndexIDConverter : System.Windows.Data.IMultiValueConverter
{
private IDDisplayList tmp = new IDDisplayList();
public object Convert( object[] values, Type targetType, object parameter, CultureInfo culture )
{
if ( values[0] == DependencyProperty.UnsetValue )
values[0] = -1;
if ( values[1] == DependencyProperty.UnsetValue )
values[1] = new IDDisplayList();
int? ajdi = (int?) values[0];
IDDisplayList lista = (IDDisplayList) values[1];
tmp = lista;
int? index = lista?.IndexOf( lista?.Where( x => x.ID == ajdi )?.FirstOrDefault() );
return index == null ? -1 : index;
}
public object[] ConvertBack( object value, Type[] targetTypes, object parameter, CultureInfo culture )
{
if ( value == DependencyProperty.UnsetValue )
value = -1;
int index = (int)value;
int? id = tmp[index].ID;
return new object[] { id, tmp };
}
}
以下是Binding
内的Page
:
<V3:ComboBox ItemsSource="{Binding Source={x:Static s:CachedData.Areas}}"
SelectedID="{Binding AreaID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,
NotifyOnTargetUpdated=True}"/>
一切都很好。打开Page
时调用转换器,正确设置SelectedIndex
属性,当我从ComboBox
中选择某个内容时,正在调用ConvertBack
。现在,我的id
和tmp
属性中的值只是我需要它的值,但SelectedID
的{{1}}属性保持不变。有没有人在这看到这个问题?