WPF - 设置工具提示 MaxWidth

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14033238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:46:53  来源:igfitidea点击:

WPF - Setting ToolTip MaxWidth

wpfxamlwpf-controlstooltip

提问by iremce

I would like to set ToolTip maxwidth property to show long texts properly. In addition I need text wrapping. I used this style:

我想设置 ToolTip maxwidth 属性以正确显示长文本。另外我需要文字换行。我使用了这种风格:

<Style TargetType="ToolTip">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding}"  MaxWidth="400" TextWrapping='Wrap' />
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
</Style>

This tooltip style is OK for my purpose. However, it is not effective for some controls which has own tooltip style. For example, tooltip of following button can not appear.

这个工具提示样式适合我的目的。但是,它对某些具有自己工具提示样式的控件无效。例如,无法显示以下按钮的工具提示。

<Button>
    <Button.ToolTip>
        <StackPanel>
            <TextBlock Style="{StaticResource firstText}" Text="aaaaaaaaaaaaa"/>
            <TextBlock Style="{StaticResource secondText}" Text="bbbbbbbbbbbbb"/>    
            <TextBlock Bacground="Red" Text="ccccccccccccc"/>    
        </StackPanel>
    </Button.ToolTip>
</Button>

I want to set maxwidth property with text wrapping for all tooltips. What can i do for this issue?

我想为所有工具提示设置带有文本换行的 maxwidth 属性。我可以为这个问题做些什么?

采纳答案by iremce

Following style of ToolTip is useful for me:

下面的 ToolTip 样式对我很有用:

<Style TargetType="ToolTip" x:Key="InternalToolTipStyle">
    <Setter Property="MaxWidth" Value="{Binding Path=(lib:ToolTipProperties.MaxWidth)}" />
    <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>

With this style, tooltip of following button appears properly:

使用此样式,以下按钮的工具提示会正确显示:

<Button>
<Button.ToolTip>
    <StackPanel>
        <TextBlock Style="{StaticResource firstText}" Text="aaaaaaaaaaaaa"/>
        <TextBlock Style="{StaticResource secondText}" Text="bbbbbbbbbbbbb"/>    
        <TextBlock Bacground="Red" Text="ccccccccccccc"/>    
    </StackPanel>
</Button.ToolTip>

回答by Lance

I avoid using Template because a lot things must be implement. So more elegant way to do that

我避免使用模板,因为必须实现很多东西。所以更优雅的方式来做到这一点

<Style TargetType="ToolTip">
    <Style.Resources>
        <Style TargetType="ContentPresenter">
            <Style.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="TextWrapping" Value="Wrap" />
                </Style>
            </Style.Resources>
        </Style>
    </Style.Resources>
    <Setter Property="MaxWidth" Value="500" />
</Style>

回答by Sheridan

I realise that this is an old question, but no one seems to have suggested the most obvious and simplest solution to this problem yet. As such, I thought I'd add it here:

我意识到这是一个老问题,但似乎还没有人提出最明显和最简单的解决方案来解决这个问题。因此,我想我会在这里添加它:

<Button>
    <Button.ToolTip>
        <ToolTip MaxWidth="400">
            <TextBlock Text="{Binding Binding}" TextWrapping="Wrap" />
        </ToolTip>
    </Button.ToolTip>
</Button>

回答by Ramin

Use this:

用这个:

<Window.Resources>
 <Style TargetType="ToolTip" x:Key="TT">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding}"  MaxWidth="400" TextWrapping='Wrap' />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
 </Style>
</Window.Resources>

<Button>
    <Button.ToolTip>
        <ToolTip Style="{StaticResource TT}">
  bbbbbbbbbbbbbbbbbbbdddddddddddddddddbbbmmmmmmhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
        </ToolTip>
    </Button.ToolTip>
</Button>

Edit:

编辑:

<Button>
    <Button.ToolTip>
        <RichTextBox>
           <FlowDocument>
              <Paragraph>
                This is flow content and you can <Bold>edit me!</Bold>
              </Paragraph>
           </FlowDocument>
</RichTextBox>
    </Button.ToolTip>
</Button>

see :RichTextBox Overview

请参阅:RichTextBox 概述