Reputation: 4532
I'm stuck on a problem and I'm sure this happened because I'm not using WPF as I should. I will try to describe my situation and would appreciate any ideas.
I'm about to create a CustomControl
that shows a numerical value and can show validation errors for this value. The control is part of a control library and does not know anything about the value or its validation but should show validation errors as a tooltip. Therefore I used the default style to add a trigger:
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors).CurrentItem.ErrorContent}" />
</Trigger>
</Style.Triggers>
What I want to do now is to give the user of the control the possibility to translate the validation error with the help of a converter. Therefore I added a DependencyProperty
to the custom control:
public IValueConverter LocConverter
{
get { return (IValueConverter)GetValue(LocConverterProperty); }
set { SetValue(LocConverterProperty, value); }
}
I was trying to use this converter via a binding:
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors).CurrentItem.ErrorContent,
Converter={Binding LocConverter,
RelativeSource={RelativeSource AncestorType={x:Type MyControl}}}}" />
But this is causing a runtime error because I cannot use a binding for a converter.
Is there another possibility to bind a converter or an idea how to pass a translation method to the control?
Upvotes: 0
Views: 121
Reputation: 25623
Typically, the best way to apply a binding that depends on two or more values which may change independently (in this case, your error content and your converter) is to use a MultiBinding
. You could write a custom IMultiValueConverter
that accepts the error content as its first value and the converter as its second value, then use the provided converter to perform the actual conversion. The MultiBinding
will be reevaluated when either the error content or your LocConverter
changes.
Upvotes: 1