0

I searched and find out I can't set the tooltip in setter.value directly (in a style.xaml file). However I can use static resource to set the tooltip.

My question is, since I need to supply dynamic text for the tooltip, I can't use static resource. How should I do that?

here is my example.

    <Style x:Key="ErrorStyleRadius" TargetType="{x:Type FrameworkElement}">
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding (Validation.HasError), RelativeSource={RelativeSource Mode=Self}}" Value="True"/>
                <Condition Binding="{Binding (Validation.Errors), RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource IsError}}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="ToolTip">
                    <Setter.Value>
                        <ToolTip>
                            <Label Content="{Binding somePropertyHere}"/>
                        </ToolTip>
                    </Setter.Value>
                </Setter>
                <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorControlTemplateRadiusError}"/>
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>

        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding (Validation.HasError), RelativeSource={RelativeSource Mode=Self}}" Value="True"/>
                <Condition Binding="{Binding (Validation.Errors), RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource IsWarning}}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors), Converter={StaticResource ValMsg}}"/>
                <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorControlTemplateRadiusWarning}"/>
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

for usage, i can use as

<textbox style={staticresource ErrorStyleRadius} text={bind name, validationOnDataError=true}/>
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
GaryX
  • 737
  • 1
  • 5
  • 20

2 Answers2

3

Why don't you try this -

<ToolTip x:Key="MyToolTip"
         DataContext={Binding PlacementTarget, RelativeSource={RelativeSource Self}}>
  <Label Content="{Binding Text}"/>
</ToolTip>

<Style x:Key="ErrorStyleRadius" TargetType="{x:Type FrameworkElement}">
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding (Validation.HasError), RelativeSource={RelativeSource Mode=Self}}" Value="True"/>
                <Condition Binding="{Binding (Validation.Errors), RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource IsError}}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="ToolTip" Value={StaticResource MyToolTip}>
                </Setter>
                <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorControlTemplateRadiusError}"/>
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

You can give the property name in StaticResource. It will update the tooltip dynamically.

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Thank you for your answer. One difference in my case is I want to bind the ToolTip text to the attached property (Validation.Errors) in the correspondent control, NOT the data context. So I need to find parent related resource to the control first. However, find parent related resource in a static resource is, I don't know how to, impossible? (because it is not in the name scope?) – GaryX Mar 16 '11 at 04:39
  • You can even bind to the attached property by setting the data context for the tooltip as its PlacementTarget(Parent RelatedResource). I have update my answer, have a look at it. I am now binding to the attached property text. Same way you can bind to the (Validation.Errors). Hope this is what you want!! – Rohit Vats Mar 16 '11 at 06:35
  • Hi, thank you for your continuous effort helping me out. To state clear, I want to bind to (Validation.Errors) "OF THE PARENT CONTROL". As in the answer, I see {RelativeSource Self}, not FindAncester. Not sure whether that still work in static resource. But anyway, I will try your way and see how to fix my problem. Thank you again. – GaryX Mar 17 '11 at 01:08
  • I bind the datacontext to the attached property "PlacementTarget". It finds the parent on which the tooltip was applied. I tried this in the small application and it worked for me, even binding to the (Validation.Errors)[0].ErrorContent did work. – Rohit Vats Mar 18 '11 at 06:37
1

Also you can set the tooltip from your style if you want to set the text to Validation.Error like this -

<Trigger Property="Validation.HasError" Value="True">
       <Setter Property="ToolTip"
               Value="{Binding RelativeSource={RelativeSource Self},
                       Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185