开发者

How to define a default tooltip style for all Controls

I would like to define a style with a template when there are validation errors and would display the first error message as a tooltip.

It works fine when targeting specific control like DatePicker in the following xaml.

<Style TargetType="{x:Type ToolKit:DatePicker}">
    <开发者_如何学PythonStyle.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip" 
                    Value="{Binding RelativeSource={RelativeSource Self},
                    Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

I cannot get it to work for Control though, i.e. the following doesn't give any tooltip

<Style TargetType="{x:Type ToolKit:Control}">
    <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>

Any idea?


I would recommend you create a Behavior for this one.

Every control is unique in itself and this is not a reliable way to attach a specific behavior to all controls. And in fact, you may end up setting this property on unwanted controls that don't even require validation.

Behaviors are clean and you can assign them to selected controls only. Attached Properties or a Master Behavior can be used to assign Behaviors to child controls.

Here is a CodeProject article from Josh Smith to get you started on Behaviors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜