开发者

cannot set tooltip in style

开发者 https://www.devze.com 2023-02-16 16:43 出处:网络
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.

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>
            <MultiDataTrig开发者_如何学Cger.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}/>


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.


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>
0

精彩评论

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