rupertmulrenan
rupertmulrenan

Reputation: 355

WPF ToolTip Trigger Not Working

I am implementing in my ViewModel to perform validation on some of my bound properties. I am then trying to use the following to set the and automatically show it:

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <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="ToolTip.IsOpen" Value="True"></Setter>
                <Setter Property="ToolTip.StaysOpen" Value="True"></Setter>
                <Setter Property="ToolTip.Placement" Value="Bottom"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style> 

The problem is that while the is being set it doesn't appear unless i put my cursor over the input. I would like to show a ToolTip automatically when validation fails.

I can think of many ways to validate data but I'm looking for a solution which is streamlined, easily reusable and doesn't clutter the rest of my code up (and the UI).If there is a better way to do this then Id like to hear about it.

Cheers

Upvotes: 3

Views: 2327

Answers (1)

J King
J King

Reputation: 4434

I agree with Viv. I used a style to define a validation.errortemplate for the textbox that is in a style. It adds a red box under the control with the error message. You can try this and adjust it. I don`t think forcing the tooltip is the right idea. I would use a popup from the validation.errortemplate if you really must have that functionality.

<Style x:Key="FTC_ValidateTextBox" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
    <Setter Property="FontFamily" Value="Open Sans Condensed"/>
    <Setter Property="FontSize" Value="19" />
    <Setter Property="Margin" Value="3,3,15,6"/>
    <Setter Property="Padding" Value="10,3"/>
    <Setter Property="TextWrapping" Value="Wrap" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Background" Value="{StaticResource DetailTextBox}" />
    <Setter Property="BorderBrush" Value="{StaticResource MediumGray}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <Border BorderBrush="{StaticResource MediumRed}" >
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <AdornedElementPlaceholder x:Name="parentTextBox" />
                        <TextBlock Grid.Row="1" Style="{StaticResource DetailError}"
                            Text="{Binding AdornedElement.(Validation.Errors).CurrentItem.ErrorContent, ElementName=parentTextBox}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip" Value="{Binding (Validation.Errors).CurrentItem.ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
            <Setter Property="BorderBrush" Value="{StaticResource MediumRed}"/>
            <Setter Property="Foreground" Value="{StaticResource MediumRed}"/>
            <Setter Property="Margin" Value="3,3,15,31"/>
        </Trigger>
    </Style.Triggers>
</Style>

Upvotes: 1

Related Questions