带转换器ConvertBack的MultiBinding从未调用

时间:2016-07-22 11:16:44

标签: c# wpf xaml binding

我在ItemsSource上有一个带有多重绑定的wpf列表框:

<ListBox Grid.Column="2"  x:Name="lstEnumValues" >
<ListBox.ItemsSource>
    <MultiBinding Converter="{StaticResource EnumValuesConverter}" Mode="TwoWay" >
        <Binding Path="AlertProperty.Model.Values" ></Binding>
        <Binding Path="AlertProperty.Values" ></Binding>
    </MultiBinding>
</ListBox.ItemsSource>
<ListBox.ItemTemplate>
    <DataTemplate>
        <CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Content="{Binding Path=Value}" />
    </DataTemplate>
</ListBox.ItemTemplate>

EnumValuesConverter是一个IMultiValueConverter,返回一个ObservableNotifiableCollection:

public class EnumValuesConverter : IMultiValueConverter {
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{

    var enumValues = values[0] as IEnumerable<string>;
    var selectedEnumValues = values[1] as IEnumerable<string>;
    var lst = new ObservableNotifiableCollection<CheckListItem>();
    if (enumValues != null)
        foreach (var item in enumValues)
        {
            var checkItem = new CheckListItem { Value = item };
            if (selectedEnumValues != null && selectedEnumValues.Contains(item))
                checkItem.IsChecked = true;

            lst.Add(checkItem);
        }

    return lst;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
    var items = value as ObservableNotifiableCollection<CheckListItem>;
    var enumValues = new ObservableCollection<string>();
    var selectedEnumValues = new ObservableCollection<string>();

    if (items != null)
    {
        foreach (var item in items)
        {
            if (item.IsChecked)
                selectedEnumValues.Add(item.Value.ToString());

            enumValues.Add(item.Value.ToString());
        }
    }

    return new object[] { enumValues, selectedEnumValues };
}}

选中内部CheckBox时,不会调用ConvertBack。

选中复选框时会正确触发CollectionChanged,但它类似于MultiBinding等待Propertychanged而不是Collectionchanged。

出了什么问题?如果MultiBinding等待属性改变,我怎么解雇它呢?

0 个答案:

没有答案