WPF - 使用样式触发器设置自定义工具提示

发布于 2024-09-16 09:29:18 字数 1466 浏览 3 评论 0原文

我正在尝试根据属性 HasValidationError 将工具提示显示到堆栈面板。

        <Style TargetType="StackPanel" x:Key="stackstyle">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasValidationError}" Value="True">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <Binding Path="DisplayError"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

该代码运行良好。但它在黄色背景下显示工具提示(与普通工具提示一样)。我需要对其进行自定义以更改并包含图像。为此,

        <Style TargetType="StackPanel" x:Key="stackstyle">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasValidationError}" Value="True">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <StackPanel>
                                 <!-- Have to add image and other decorations here -->
                                 <TextBlock Text = "{Binding DisplayError}"/>
                            </StackPanel>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

将 StackPanel 添加到 .请帮我解决。

I am trying to display tool tip to a stack panel based on property HasValidationError.

        <Style TargetType="StackPanel" x:Key="stackstyle">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasValidationError}" Value="True">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <Binding Path="DisplayError"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

The code works fine. But it displays the tooltip under yellow background ( as normal tooltip). I need to customize it to change and include image. For that,

        <Style TargetType="StackPanel" x:Key="stackstyle">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasValidationError}" Value="True">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <StackPanel>
                                 <!-- Have to add image and other decorations here -->
                                 <TextBlock Text = "{Binding DisplayError}"/>
                            </StackPanel>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

It shows error when adding StackPanel to the . Please help me in solving.

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

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

发布评论

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

评论(1

八巷 2024-09-23 09:29:18

我不知道为什么会失败,但您可以通过将 ToolTip 设置为资源来解决此问题:

<StackPanel x:Key="ToolTipContents">
    <!-- Have to add image and other decorations here -->
    <TextBlock Text = "{Binding DisplayError}"/>
</StackPanel>
<Style TargetType="StackPanel" x:Key="stackstyle">
    <Style.Triggers>
        <DataTrigger Binding="{Binding HasValidationError}" Value="True">
            <Setter Property="ToolTip" Value="{StaticResource ToolTipContents}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

或者

<ToolTip x:Key="ToolTipContents">
    <StackPanel>
        <!-- Have to add image and other decorations here -->
        <TextBlock Text = "{Binding DisplayError}"/>
    </StackPanel>
</ToolTip>
<!-- etc -->

此外,您拥有的代码将按照 .NET 4 中编写的方式工作,因此该错误已得到修复。

I don't know why that fails, but you can work around it by making the ToolTip a resource:

<StackPanel x:Key="ToolTipContents">
    <!-- Have to add image and other decorations here -->
    <TextBlock Text = "{Binding DisplayError}"/>
</StackPanel>
<Style TargetType="StackPanel" x:Key="stackstyle">
    <Style.Triggers>
        <DataTrigger Binding="{Binding HasValidationError}" Value="True">
            <Setter Property="ToolTip" Value="{StaticResource ToolTipContents}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

or

<ToolTip x:Key="ToolTipContents">
    <StackPanel>
        <!-- Have to add image and other decorations here -->
        <TextBlock Text = "{Binding DisplayError}"/>
    </StackPanel>
</ToolTip>
<!-- etc -->

Also, the code you have will work as written in .NET 4, so the bug has been fixed.

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