C# 如果需要,显示 WPF 工具提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1115924/
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-08-06 08:34:41  来源:igfitidea点击:

Show WPF Tooltip if needed

c#wpfconditionaltooltip

提问by Martin Konicek

I have a TextBlock inside a limited-size control. If the text is too long to fit into the control, I'd like to show a tooltip with full text. This is a classic behavior you surely know from many apps.

我在大小有限的控件中有一个 TextBlock。如果文本太长而不适合控件,我想显示带有全文的工具提示。这是您从许多应用程序中肯定知道的经典行为。

I tried using a Converter to convert TextBlock width into Tooltip's Visibility.

我尝试使用转换器将 TextBlock 宽度转换为工具提示的可见性。

<GridViewColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Text}">
            <TextBlock.ToolTip>
                <ToolTip 
                    DataContext="{TemplateBinding Content}" 
                    Visibility="{Binding Converter={StaticResource visConvert}}">

                        <TextBlock Text="{Binding Text}"></TextBlock>
                </ToolTip>
            </TextBlock.ToolTip>
        </TextBlock>
    </DataTemplate>
</GridViewColumn.CellTemplate>

The problem is that in the Converter:

问题是在转换器中:

public object Convert(object value, ...

'value' is the DataBound item. I'd like the 'value' to be the TextBlock, to observe its Width, and compare it to the GridViewColumn.Width.

'value' 是 DataBound 项。我希望“值”是 TextBlock,以观察其宽度,并将其与 GridViewColumn.Width 进行比较。

采纳答案by Martin Konicek

I figured it out, the Tooltip has PlacementTargetproperty that specifies the UI element that has the Tooltip. In case anyone needs it:

我想通了,工具提示具有PlacementTarget属性,该属性指定具有工具提示的 UI 元素。如果有人需要它:

<TextBlock Text="{Binding Text}">
    <TextBlock.ToolTip>
        <ToolTip 
             DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}" 
             Visibility="{Binding Converter={StaticResource toolVisConverter}}">
             <TextBlock Text="{Binding Text}"/>  <!-- tooltip content -->
         </ToolTip>
    </TextBlock.ToolTip>
</TextBlock>

And then write a Converter that converts TextBlock to Visibility (based on TextBlock width).

然后编写一个转换器,将 TextBlock 转换为 Visibility(基于 TextBlock 宽度)。

回答by Zyphrax

I would think you have to look at a ControlTemplate trigger to solve this problem. Unfortunately ControlTemplate triggers always compare with a specific value, not less than or greater than. You can make it appear e.g. if the Width = 100, not Width < 100.

我认为您必须查看 ControlTemplate 触发器才能解决此问题。不幸的是 ControlTemplate 触发器总是与特定值进行比较,而不是小于或大于。你可以让它出现,例如如果宽度 = 100,而不是宽度 < 100。

回答by Martin Konicek

Ok, so why do it the hard XAML-only way? This works:

好的,那么为什么要采用仅 XAML 的硬方式呢?这有效:

<TextBlock Text="{Binding Text}"
     IsMouseDirectlyOverChanged="TextBlock_IsMouseDirectlyOverChanged" >
     <TextBlock.ToolTip>
     <ToolTip Visibility="Collapsed">
         <TextBlock Text="{Binding Text}"></TextBlock>
     </ToolTip>
     </TextBlock.ToolTip>
</TextBlock>

in Control.xaml.cs:

在 Control.xaml.cs 中:

private void TextBlock_IsMouseDirectlyOverChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    bool isMouseOver = (bool)e.NewValue;
    if (!isMouseOver)
        return;
    TextBlock textBlock = (TextBlock)sender;
    bool needed = textBlock.ActualWidth > 
        (this.listView.View as GridView).Columns[2].ActualWidth;
    ((ToolTip)textBlock.ToolTip).Visibility = 
        needed ? Visibility.Visible : Visibility.Collapsed;
}