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

3
我正在尝试根据属性 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到中时出现错误。请帮我解决。
1个回答

5

我不知道为什么会失败,但你可以通过将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的编写方式正常工作,因此错误已被修复。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接