【问题标题】:WPF Tooltip dimensions not fitting contentWPF 工具提示尺寸不适合内容
【发布时间】:2014-12-19 19:25:16
【问题描述】:

我遇到了在验证文本框时显示的 WPF 工具提示问题。鼠标悬停时显示的工具提示看起来像一个大矩形,而不是环绕文本。我正在使用这种风格:

        <Style TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                            Path=(Validation.Errors)[0].ErrorContent}"/>
                    <Setter Property="TextBox.Background" Value="Red" /> 
                </Trigger>
            </Style.Triggers>
        </Style>

有什么建议吗?谢谢。

【问题讨论】:

    标签: wpf validation xaml styles tooltip


    【解决方案1】:

    您可以为工具提示创建样式并使用 TextBlock 添加控件模板。在 TextBlock 中进行文本换行。唯一的问题是您可能需要为工具提示设置 MaxWidth

    <Style TargetType="ToolTip">
                <Setter Property="MaxWidth" Value="300" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <ContentPresenter Content="{TemplateBinding Content}"  >
                                <ContentPresenter.Resources>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="TextWrapping" Value="Wrap" />
                                    </Style>
                                </ContentPresenter.Resources>
                            </ContentPresenter>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    

    【讨论】:

    • 谢谢。是的,我确实手动设置了宽度,但我们不能将它绑定到 TextBlock 的高度吗?如果没记错的话,工具提示尺寸不应该自动执行吗?
    • 在 WPF 中,如果我们没有提到任何宽度或高度的文本,它是不可能的。滚动条也是如此。
    • 看了这个博客,看到不用设置样式,tooltip就可以了。它是如何做到的? c-sharpcorner.com/UploadFile/princy.scorpin/…
    • 哦是吗?!不公平。好的,那么看来我们将不得不设置它的 MaxWidth,MaxHeight
    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 2018-12-13
    相关资源
    最近更新 更多