WPF - 设置工具提示MaxWidth

ire*_*mce 16 wpf xaml tooltip wpf-controls

我想设置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>
Run Code Online (Sandbox Code Playgroud)

这个工具提示风格适合我的目的.但是,对于某些具有自己工具提示风格的控件来说,它无效.例如,无法显示以下按钮的工具提示.

<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>
Run Code Online (Sandbox Code Playgroud)

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

小智 24

我避免使用模板,因为必须实现很多东西.这样更优雅的方式

<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>
Run Code Online (Sandbox Code Playgroud)

  • 这实际上是一个非常好的清洁的解决方案,具有很多重用性。我认为在TargetType中使用x:Type也更加严格和简洁。 (2认同)

She*_*dan 19

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

<Button>
    <Button.ToolTip>
        <ToolTip MaxWidth="400">
            <TextBlock Text="{Binding Binding}" TextWrapping="Wrap" />
        </ToolTip>
    </Button.ToolTip>
</Button>
Run Code Online (Sandbox Code Playgroud)

  • 最简单的一种解决方案! (2认同)

ire*_*mce 10

以下样式的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>
Run Code Online (Sandbox Code Playgroud)

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

<Button>
<Button.ToolTip>
    <StackPanel>
        <TextBlock Style="{StaticResource firstText}" Text="aaaaaaaaaaaaa"/>
        <TextBlock Style="{StaticResource secondText}" Text="bbbbbbbbbbbbb"/>    
        <TextBlock Bacground="Red" Text="ccccccccccccc"/>    
    </StackPanel>
</Button.ToolTip>
Run Code Online (Sandbox Code Playgroud)