I'm trying to add a property, that can be attached to any control and bind a value to it.
public class ValidationBorder : DependencyObject
{
public static readonly DependencyProperty HasErrorProperty =
DependencyProperty.Register(
"HasError",
typeof(bool?),
typeof(UIElement),
new PropertyMetadata(default(Boolean))
);
public bool? HasError
{
get { return (bool?) GetValue(HasErrorProperty); }
set { SetValue(HasErrorProperty, value);}
}
public static void SetHasError(UIElement element, Boolean value)
{
element.SetValue(HasErrorProperty, value);
}
public static Boolean GetHasError(UIElement element)
{
return (Boolean)element.GetValue(HasErrorProperty);
}
}
My usage:
<TextBox Text="{Binding SelectedFrequencyManual, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Center"
attached:ValidationBorder.HasError="{Binding Path=DataOutOfRange}">
</TextBox>
When I start the project it displays error (translated):
A Binding cannot be specified in the TextBox setHasError property. Only a Binding can be specified in a DependencyProperty of a DependencyObject
What can be wrong with it?
I've tried everything I could find on the web:
- Add parenthesis in the binding
- Add RelativeSource
- Change
DependencyProperty.Register
toDependencyProperty.RegisterAttached
- Different types for
typeof(UIElement)
includingtypeof(TextBox)
typeof(UIElement)
is wrong, it should betypeof(ValidationBorder)
– SinatrRegisterAttached()
method indeed. – Sinatr