It is not possible to bind a value to ConverterParametr
of Binding
. Binding
can be only set on a DependencyProperty
of a DependencyObject
.
I'm curious about implementing IValueConverter
converter as DependencyObject
.
public class AddConverter : DependencyObject, IValueConverter
{
public static readonly DependencyProperty AddIntegerProperty =
DependencyProperty.Register(nameof(AddInteger),
typeof(int),
typeof(AddConverter),
new PropertyMetadata(0));
public int AddInteger
{
get => (int)GetValue(AddIntegerProperty);
set => SetValue(AddIntegerProperty, value);
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is int intValue)) return 0;
return intValue + AddInteger;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
int intValue;
try
{
intValue = System.Convert.ToInt32(value);
}
catch (Exception)
{
return 0;
}
return intValue - AddInteger;
}
}
Let's us it in example View.
<TextBox>
<TextBox.Text>
<Binding Path="MyIntegerProperty">
<Binding.Converter>
<local:AddConverter AddInteger="{Binding MyAddIntegerProperty, Mode=OneWay}" />
</Binding.Converter>
</Binding>
</TextBox.Text>
</TextBox>
The result is that AddInteger
still returns the default value. What is the reason that there is no change of AddInteger
Dependency Property via provided Binding
?
Footnote: The MultiBinding
does not help me because of ConvertBack
method consists only of value provided by a control. This stuff should also be solved in ViewModel, but I'm curious about a solution with a converter.