Guffel
Guffel

Reputation: 45

How to show text of a textbox in a tooltip if no validation error occurs. Otherwise show the validation error

I have a textbox which validates a date.

I want to show the content in a tooltip on the textbox if it is valid. Otherwise I want to show the validation error in a tooltip.

I've set the standard tooltip on Text and added a couple of trigger:

<MultiTrigger>
   <MultiTrigger.Conditions>
      <Condition Property="Validation.HasError"
                 Value="False" />
      <Condition Property="Text"
                 Value="" />
    </MultiTrigger.Conditions>
    <MultiTrigger.Setters>
       <Setter Property="ToolTipService.ToolTip"
               TargetName="DataTextBox"
               Value="{x:Null}"/>
    </MultiTrigger.Setters>
</MultiTrigger>

<MultiTrigger>
   <MultiTrigger.Conditions>
      <Condition Property="Validation.HasError"
                 Value="True" />
    </MultiTrigger.Conditions>
    <MultiTrigger.Setters>
       <Setter Property="ToolTipService.ToolTip"
               TargetName="DataTextBox"
               Value="{Binding (Validation.Errors)[0].ErrorContent}"/>
    </MultiTrigger.Setters>
</MultiTrigger>

I need something like a negated Condition to check if text is added in the textbox.

Thank you for your help.

Upvotes: 2

Views: 1214

Answers (1)

Rachel
Rachel

Reputation: 132658

Just bind your ToolTip to the TextBox's Text by default, and use a Trigger to set the Validation error if the item has an error.

<Style x:Key="MyTextBoxStyle" TargetType="{x:Type TextBox}">
    <Setter Property="ToolTip" 
            Value="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />

    <Style.Triggers>
       <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip" Value="{Binding 
                Path=(Validation.Errors)[0].ErrorContent, 
                RelativeSource={RelativeSource Self}}" />
        </Trigger>
    </Style.Triggers>
</Style>

Also, you shouldn't use a MultiDataTrigger unless you're evaluating more than one condition

Upvotes: 4

Related Questions