WPF绑定到可空整数DP

时间:2017-09-20 14:42:24

标签: wpf xaml combobox binding imultivalueconverter

出于某种原因,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。现在,我的idtmp属性中的值只是我需要它的值,但SelectedID的{​​{1}}属性保持不变。有没有人在这看到这个问题?

0 个答案:

没有答案