IValueConverter并绑定DependencyObject [英] IValueConverter and binding a DependencyObject

查看:93
本文介绍了IValueConverter并绑定DependencyObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ComboBox ,我需要对 SelectedItem 进行转换。问题是 IValueConverter 需要绑定值,但也需要Collection。配置了 DependencyObject ,但它给我一个错误消息

I have a ComboBox that I need to do a converter on the SelectedItem. Problem is the IValueConverter needs the binding value but a Collection as well. Configured a DependencyObject but it is giving me an error message of


对象类型'System.Windows.Data.Binding'不能转换为'System.Collections.ObjectModel.ObservableCollection`1 [MyClass]'类型。

Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Collections.ObjectModel.ObservableCollection`1[MyClass]'.

这是我的 IValueConverter

public class MyConverter : DependencyObject, IValueConverter
{
    public static readonly DependencyProperty FoldersProperty = 
        DependencyProperty.Register(nameof(MyCollection), typeof(ObservableCollection<MyClass>), typeof(MyClassModelToMyClassID), new FrameworkPropertyMetadata(new ObservableCollection<MyClass>()));
    public ObservableCollection<MyClass> MyCollection
    {
        get { return GetValue(FoldersProperty) as ObservableCollection<MyClass>; }
        set { SetValue(FoldersProperty, value); }
    }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //Amazing Convert code that uses MyCollection and Value
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //Amazing ConvertBack code that uses MyCollection and Value
    }
}

这是我的称呼方式:

<Page.Resources>
    <converter:MyConverter x:Key="Converter" MyCollection="{Binding DataCollection}" />
</Page.Resources>
....
<ComboBox 
    ItemsSource="{Binding DataCollection}"
    SelectedItem="{Binding Path=MyValue, Converter={StaticResource TaxCodeConverter}}" />

编辑:添加完整的IValueConvert减去Convert和ConvertBack代码

edit: added the full IValueConvert subtracted the Convert and ConvertBack code

推荐答案

像BindingProxy一样,它必须是一个Freezable 。另外,请勿将新的可观察到的集合传递给您的元数据。这是一种引用类型,因此此转换器的所有实例都将使用相同的实际集合实例进行初始化。

Like a BindingProxy, it needs to be a Freezable. Also, don't pass a new observable collection to your metatadata. That's a reference type, so all instances of this converter will be initialized with the same actual collection instance.

让我知道您是否遇到其他问题,但是我已经做到了,并且能够绑定到依赖项属性。

Let me know if you run into some other issue, but I've done this and been able to bind to the dependency property.

许多人认为更好的方法是多绑定和多值转换器。我认为,使用具有描述性名称的强类型属性是有价值的。

Many would argue that a better approach would be a multibinding and a multi-value converter. I think there's value in having a strongly typed property with a descriptive name.

public class MyConverter : Freezable, IValueConverter
{
    /* omitted: Convert() and ConvertBack() */

    public MyConverter()
    {
        //  Initialize here if you need to
        MyCollection = new ObservableCollection<MyClass>();
    }

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

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

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

XAML的使用与您所使用的一样您的问题:绑定依赖项属性,并且绑定将更新 MyConverter 的该实例的该属性,只要您的Page的DataContext具有名为 DataCollection 。

XAML usage will be just as you have it in your question: Bind the dependency property, and the binding will update that property of that instance of MyConverter, provided that your Page's DataContext has an appropriately typed property named DataCollection.

<Page.Resources>
    <converter:MyConverter x:Key="Converter" MyCollection="{Binding DataCollection}" />
</Page.Resources>

这篇关于IValueConverter并绑定DependencyObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆