无法在样式中设置工具提示

发布于 2024-10-21 19:59:52 字数 2328 浏览 1 评论 0原文

我搜索并发现我无法直接在 setter.value 中设置工具提示(在 style.xaml 文件中)。不过我可以使用静态资源来设置工具提示。

我的问题是,由于我需要为工具提示提供动态文本,因此我无法使用静态资源。我该怎么做呢?

这是我的例子。

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

对于使用,我可以用作

<textbox style={staticresource ErrorStyleRadius} text={bind name, validationOnDataError=true}/>

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}/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小傻瓜 2024-10-28 19:59:52

为什么不试试这个 -

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

您可以在 StaticResource 中给出属性名称。它将动态更新工具提示。

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.

岁月如刀 2024-10-28 19:59:52

如果您想将文本设置为 Validation.Error ,您也可以从您的样式中设置工具提示,如下所示 -

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文