nadim-241
nadim-241

Reputation: 9

Get control which style is applied to from within a control template inside the style

I have a style for a TextBox which contains a ControlTemplate. The idea is that when a textbox using this style is in error, the ControlTemplate is applied to it, and this all works. What I would like to do is preserve the Tooltip of the Textbox which this style is applied to, so that controls inside the ControlTemplate can display the same ToolTip when they are interacted with. The issue is that I cannot find a way to get the Control which the style is applied to from inside the style.

I've tried setting the ToolTip for one of the controls inside the ControlTemplate as follows:

<ToolTip Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ToolTip}"/>

But this just gives me a blank ToolTip (I think TemplatedParent only works when the ControlTemplate is not inside a style for some reason).

I know I could probably do this manually for each textbox, but I have in excess of 20 textboxes, so that doesn't seem like the most efficient way to do it. I suspect I am misunderstanding something fundamental about how TemplatedParent works.

Textbox style:

<Style x:Key="TextBoxInError" TargetType="TextBox"> 
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid VerticalAlignment="Center" HorizontalAlignment="Right" Name="TemplateGrid">
                            <TextBlock HorizontalAlignment="Right" Margin="10,0,10,0" VerticalAlignment="Center"
                                       Foreground="Red" FontSize="20" FontFamily="Segoe MDL2 Assets" Text="&#xE7BA;"
                                       PreviewMouseDown="ExclamationPointMouseDown"
                                       ToolTip="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ToolTip}">
                            </TextBlock>
                            <AdornedElementPlaceholder />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="ToolTip"
                                Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                  Path=(Validation.Errors)[0].ErrorContent}" />
                    <Setter Property="BorderBrush" Value="Red" />
                    <Setter Property="BorderThickness" Value="2" />
                </Trigger>
                <Trigger Property="Validation.HasError"
                         Value="false">
                    <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=Tag}" />
                    <Setter Property="BorderBrush" Value="Gainsboro" />
                    <Setter Property="BorderThickness" Value="1" />
                </Trigger>
            </Style.Triggers>
        </Style>

Upvotes: 0

Views: 35

Answers (0)

Related Questions