I'm working with WPF, and I'm trying to use Attached Properties to work out some aesthetic details of my controls (specifically, regarding validations).
To explain my problem I must add some code..
My Attached Property
public class ToolTipExtension
{
public static readonly DependencyProperty ShowToolTipProperty = DependencyProperty.RegisterAttached(
"ShowToolTip", typeof(string), typeof(ToolTipExtension), new PropertyMetadata("Deffault"));
public static void SetShowToolTip(DependencyObject element, string value)
{
element.SetValue(ShowToolTipProperty, value);
}
public static string GetShowToolTip(DependencyObject element)
{
return (string) element.GetValue(ShowToolTipProperty);
}
}
My dictionary is something simple, like the following:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:at="clr-namespace:CarpetaTecnicaWPF.AttachedProperties"
>
<Style TargetType="{x:Type TextBox}" x:Key="Blah" >
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Grid>
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(at:ToolTipExtension.ShowToolTip)}" FontSize="50"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
As you can see in the dictionary, I try to bind the property Text
with the value of my Attached Property, I apply the style as follows:
<TextBox Style="{StaticResource Blah}" at:ToolTipExtension.ShowToolTip="Prueba?"/>
The problem is the following, the value Prueba?
simply does not appear, and when trying to use the Dynamic Visual Tree, I find the following:
Everything is perfect, the property has the value that I expect.
But to my surprise, the view renders the following:
It should be clarified, that it Deffault
is the default value of my AttachedProperty
The binding is incorrect.
He
TemplatedParent
is not correct in this case. TheControlTemplate
for the error does not apply to itTextBox
, it isControlTemplate
independent, therefore... it is never set, so it returns the default value of theFrameworkElement
one being queried.To access the information
TextBox
from theControlTemplate
error it is necessary to useAdornedElementPlaceholder
, from this, we can access the properties ofTextBox
through the propertyAdornedElement
.For example:
Answer from dymanoid on SOen