繁体   English   中英

如何将绑定列表作为参数传递给 IValueConverter?

[英]How to pass a binded List as parameter to a IValueConverter?

我使用 CheckBoxes ComboBox 进行多项选择,如下所示:

<ComboBox  ItemsSource="{Binding Animals}">
    <ComboBox.Resources>
        <Style TargetType="ComboBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBoxItem">
                        <CheckBox
                            IsChecked="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType={x:Type ComboBoxItem}}, Converter={StaticResource checkboxConverter}}"
                            Command="{Binding Path=DataContext.AgregarRolCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
                            <CheckBox.CommandParameter>
                                <MultiBinding Converter="{StaticResource multiSelectionConverter}">
                                    <Binding Path="IsChecked" RelativeSource="{RelativeSource Self}"/>
                                    <Binding Path="DataContext" RelativeSource="{RelativeSource AncestorType={x:Type ComboBoxItem}}"/>
                                </MultiBinding>
                            </CheckBox.CommandParameter>
                            <ContentPresenter />
                        </CheckBox>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ComboBox.Resources>
</ComboBox>

ComboBox 正在显示我在 ViewModel 中声明的动物列表,如下所示:

 public class ViewModel: ViewModelBase {
   public List < string > Animals {
    get {
     return new List < string > {
      "CAT",
      "GIRAFFE",
      "ELEPHANT",
      "DOG"
     };
    }
   }
   public List < string > SelectedAnimals {
    get {
     return new List < string > {
      "DOG",
      "CAT"
     };
    }
   }

我想要做的是,从一个名为 SelectedAnimals 的列表中,如果选择动物以显示 ComboBox 内的复选框为选中状态。 为此,我为 CheckBox 的 IsChecked 属性创建了一个名为 CheckBoxCheckedConverter 的转换器,我想在其中向转换器发送该 ComboBoxItem 的当前动物和所选动物的列表,并比较当前动物是否在所选动物列表中。

我在 UserControl.Resources 中的 xaml 中声明转换器,如下所示:

 <UserControl.Resources>
        <converters:CheckBoxCheckedConverter x:Key="checkboxConverter" MyCollection="{Binding SelectedAnimals}"/>
        ...
</UserControl.Resources>

为了在我的转换器中接收 SelectedAnimals 列表,我在这篇文章之后完成了下一个代码:

public class CheckBoxCheckedConverter : Freezable, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (MyCollection.Contains(value.ToString()))
                return true;
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public CheckBoxCheckedConverter()
        {
            MyCollection = new ObservableCollection<string>();
        }

        protected override Freezable CreateInstanceCore()
        {
            return new CheckBoxCheckedConverter();
        }


        public static readonly DependencyProperty MyCollectionProperty =
        DependencyProperty.Register(nameof(MyCollection),
            typeof(ObservableCollection<string>), typeof(CheckBoxCheckedConverter),
            new FrameworkPropertyMetadata(null));

        public ObservableCollection<string> MyCollection
        {
            get { return GetValue(MyCollectionProperty) as ObservableCollection<string>; }
            set { SetValue(MyCollectionProperty, value); }
        }
    }

问题是 MyCollection 在转换器中以 null 的形式出现,并且由于尝试将 a.Contains() 转换为 null object,因此转换给我带来了错误。 为什么转换器中的 MyCollection 变量为 null? 这种方法是正确的还是更好或更简单的方法?

问题是我传递了一个列表,而在转换器中我声明了一个 ObservableCollection。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM