【问题标题】:Converter not getting called at runtime转换器在运行时没有被调用
【发布时间】:2017-04-05 08:20:15
【问题描述】:

我有依赖属性选定项的用户控制(它是枚举值的集合)

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 items 中添加值,转换器会被调用,但如果我在按钮单击上添加值,它不会被调用。谁能告诉我它发生的原因以及我是如何可以纠正这个。

【问题讨论】:

    标签: c# wpf ivalueconverter multibinding


    【解决方案1】:

    转换器的Convert 方法仅在您绑定到的任何属性发生更改时才会被调用。您正在绑定到CheckBoxContent 属性和UserControlSelectedItems 属性。当您向集合中添加新项目时,这些都不会发生变化。

    尝试同时绑定到集合的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>
    

    【讨论】:

      猜你喜欢
      • 2019-04-11
      • 2011-03-27
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 2012-02-11
      相关资源
      最近更新 更多