wpf 在 IValueConverter 类中定义属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28141791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:52:33  来源:igfitidea点击:

Defining a Property in a IValueConverter class

wpfdependency-propertiesivalueconverter

提问by RBasniak

I need to define a DependencyProperty in a converter class because I need this data to make the conversion and this data is in another object, not the one I'm binding to.

我需要在转换器类中定义一个 DependencyProperty,因为我需要这些数据来进行转换,而这些数据位于另一个对象中,而不是我要绑定到的对象中。

My converter class is the following:

我的转换器类如下:

public class LEGOMaterialConverter   : DependencyObject, IValueConverter
{
    public DependencyProperty MaterialsListProperty = DependencyProperty.Register("MaterialsList", typeof(Dictionary<int, LEGOMaterial>), typeof(LEGOMaterialConverter));

    public Dictionary<int, LEGOMaterial> MaterialsList
    {
        get
        {
            return (Dictionary<int, LEGOMaterial>)GetValue(MaterialsListProperty);
        }
        set
        {
            SetValue(MaterialsListProperty, value);
        }
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        LEGOMaterial material = null;

        MaterialsList.TryGetValue((int)value, out material);

        return material;
    }

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

Then I'm instanciating it on the Window.REsources area:

然后我在 Window.REsources 区域实例化它:

<Window.Resources>
    <local:LEGOMaterialConverter x:Key="MaterialsConverter" MaterialsList="{Binding Path=Materials}" />
</Window.Resources>

I'm getting the following error:

我收到以下错误:

   'MaterialsList' property was already registered by 'LEGOMaterialConverter'.

Does anyone have a clue on this error?

有没有人有关于这个错误的线索?

回答by Amit Raz

Try doing it like this (just an example):

尝试这样做(只是一个例子):

public class ValueConverterWithProperties : MarkupExtension, IValueConverter
{
    public int TrueValue { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((int) value == TrueValue)
        {
            return true;
        }
        return false;
    }

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

notice I derive from markup extension to allow me to use it like this:

请注意,我从标记扩展派生而来,允许我像这样使用它:

<Window x:Class="Converter.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:converter="clr-namespace:Converter"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <CheckBox IsChecked="{Binding item, Converter={converter:ValueConverterWithProperties TrueValue=5}}"></CheckBox>
    <CheckBox IsChecked="{Binding item2, Converter={converter:ValueConverterWithProperties TrueValue=10}}"></CheckBox>
</Grid>

回答by Tom Ladek

Btw, this error is caused by your dependency property in the converter not being static (and after having created an instance of this converter somewhere before).

顺便说一句,此错误是由于转换器中的依赖属性不是静态的(并且之前在某处创建了此转换器的实例之后)。

EDIT

编辑

So the problem is with this line:

所以问题出在这一行:

public DependencyProperty MaterialsListProperty = DependencyProperty.Register("MaterialsList", typeof(Dictionary<int, LEGOMaterial>), typeof(LEGOMaterialConverter));

Here the Dependency Property MaterialsListPropertyis being registered with every instantiationof an object of this type (i.e. LEGOMaterialConverter).

在这里,依赖属性MaterialsListProperty被注册为这种类型的对象的每个实例化(即LEGOMaterialConverter)。

However Dependency Properties should be defined static, like so:

但是依赖属性应该被定义为静态的,像这样:

public static readonly DependencyProperty MaterialsListProperty = DependencyProperty.Register("MaterialsList", typeof(Dictionary<int, LEGOMaterial>), typeof(LEGOMaterialConverter));

A static variable is initialized (and the Dependency Property is registered) only once for all future instances of this type and that is what we need here. Failing to declare a Dependency Property as static results in the error from above.

对于这种类型的所有未来实例,静态变量仅被初始化一次(并且依赖属性被注册),这就是我们在这里需要的。未能将依赖属性声明为静态会导致上述错误。