转换器在运行时未被调用

Converter not getting called at runtime

我有依赖于 属性 个选定项的用户控件(这是枚举值的集合)

public IEnumerable SelectedItems
{
    get { return (IEnumerable)GetValue(SelectedItemsProperty); }
    set { SetValue(SelectedItemsProperty, value); }
}

public static readonly DependencyProperty SelectedItemsProperty =
    DependencyProperty.Register("SelectedItems", typeof(IEnumerable),
      typeof(UserControl1), new FrameworkPropertyMetadata(OnChangeSelectedItems)
      {
          BindsTwoWayByDefault = true
      });


private static void OnChangeSelectedItems(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var uc = d as UserControl1;

    if (uc != null)
    {
        var oldObservable = e.OldValue as INotifyCollectionChanged;
        var newObservable = e.NewValue as INotifyCollectionChanged;
        if (oldObservable != null)
        {
            oldObservable.CollectionChanged -= uc.SelectedItemsContentChanged;
        }
        if (newObservable != null)
        {
            newObservable.CollectionChanged += uc.SelectedItemsContentChanged;
            uc.UpdateMultiSelectControl();

        }
    }

}

private void SelectedItemsContentChanged(object d, NotifyCollectionChangedEventArgs e)
{
    UpdateMultiSelectControl();
}

我已使用转换器将 selectedItems 依赖项 属性 与我的用户控件中的复选框绑定

<ItemsControl ItemsSource="{Binding Items}" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <CheckBox x:Name="ChkBox" Margin="13 0 0 10"  Content="{Binding}" >
                    <CheckBox.IsChecked>
                        <MultiBinding Converter="{StaticResource CheckBoxConverter}"  Mode="TwoWay" >
                            <Binding ElementName="ChkBox" Path="Content" />
                            <Binding  RelativeSource="{RelativeSource FindAncestor,AncestorType={x:Type UserControl}}"  Path="DataContext.SelectedItems" UpdateSourceTrigger="PropertyChanged"  Mode="TwoWay"></Binding>
                        </MultiBinding>
                    </CheckBox.IsChecked>

                </CheckBox>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl> 

和转换器

public class CheckBoxConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType,
           object parameter, System.Globalization.CultureInfo culture)
    {
        if (values != null && values.Length > 1)
        {
            var content = values[0];
            var selected = values[1] as IList;

            if (selected != null && selected.Contains(content))
            {
                return true;
            }
        }

        return false;

    }
    public object[] ConvertBack(object value, Type[] targetTypes,
           object parameter, System.Globalization.CultureInfo culture)
    {
        return new[] { Binding.DoNothing, Binding.DoNothing };
    }
}

我的问题是,如果我在构造时在 Selected 项目中添加值,转换器将被调用,但如果我在单击按钮时添加值,它不会 called.Can 有人告诉我为什么它的原因发生了什么以及我该如何纠正这个问题。

转换器的 Convert 方法只有在您绑定到的任何属性发生更改时才会被调用。您正在绑定 CheckBoxContent 属性 和 UserControlSelectedItems 属性。当您向 collection.

添加新项目时,这些都不会改变

也尝试绑定到 collection 的 Count 属性:

<CheckBox x:Name="ChkBox" Margin="13 0 0 10"  Content="{Binding}" >
    <CheckBox.IsChecked>
        <MultiBinding Converter="{StaticResource CheckBoxConverter}"  Mode="TwoWay" >
            <Binding ElementName="ChkBox" Path="Content" />
            <Binding RelativeSource="{RelativeSource AncestorType={x:Type UserControl}}" Path="SelectedItems"/>
            <Binding RelativeSource="{RelativeSource AncestorType={x:Type UserControl}}" Path="SelectedItems.Count"/>
        </MultiBinding>
    </CheckBox.IsChecked>
</CheckBox>