Defining a property in the IValueConverter class

I need to define DependencyProperty in the converter class, because I need this data to do the conversion, and this data is in a different object, and not the one with which I am attached.

My converter class is as follows:

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 run it in the Window.REsources area:

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

I get the following error:

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

Does anyone know this error?

+5
source share
2 answers

Try to do 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(); } } 

Please note that I am getting a markup extension to allow me to use it as follows:

 <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> 

+6
source

Btw, this error is caused by the fact that your dependency property in the converter is not static (even after an instance of this converter was created earlier).

EDIT

So the problem is in this line:

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

Here, the MaterialsListProperty dependency property is registered with every instance of an object of this type (i.e., LEGOMaterialConverter ).

However, dependency properties must be defined as static:

 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 this is what we need here. It is not possible to declare a dependency property as the static result of an error from above.

+4
source

Source: https://habr.com/ru/post/1211892/


All Articles