KrisTrip
KrisTrip

Reputation: 5043

TextBox Validation ToolTip Not Appearing

I have the following Style for a TextBox:

<Style x:Key="RenamingTextBox" TargetType="{x:Type TextBox}">
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Grid>
                            <TextBlock x:Name="block" Visibility="Visible" Text="{TemplateBinding Text}" Margin="0"/>
                            <TextBox x:Name="box" Visibility="Collapsed" 
                                     Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                     Margin="1.25"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding IsRenaming}" Value="true">
                                <DataTrigger.Setters>
                                    <Setter TargetName="block" Property="TextBox.Visibility" Value="Collapsed" />
                                    <Setter TargetName="box" Property="TextBox.Visibility" Value="Visible" />
                                    <Setter TargetName="box" Property="FocusManager.FocusedElement" Value="{Binding ElementName=box}" />
                                    <Setter TargetName="box" Property="behaviors:TextBoxBehavior.SelectAll" Value="True"/>
                                </DataTrigger.Setters>
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>

and also:

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Margin" Value="5" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="BorderBrush" Value="LightGray" />
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip" 
                    Value="{Binding RelativeSource={RelativeSource Self},
                    Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

The renaming textbox works great, it changes back and forth from TextBox to TextBlock with no problem. It even gets the nice red border when there is an error in my Name. The problem is that the ToolTip doesn't show up when I hover over the textbox with an error. All my other TextBoxes in the project work fine. Any ideas?

Upvotes: 2

Views: 2046

Answers (1)

SvenG
SvenG

Reputation: 5205

The renamingTextBox won't receive the second style, because it can't get multiple styles.

Just include the "default" TextBox style in your renaming textBox Style with the BasedOn attribute:

<Style TargetType="{x:Type TextBox}">
  ...
</Style>

<Style x:Key="RenamingTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
  ...
</Style>

Upvotes: 4

Related Questions