开发者

Tooltip binding to text - How to avoid a small rectangles when text is blank

开发者 https://www.devze.com 2023-01-30 02:20 出处:网络
I have bound the Tooltip property of a control in wpf to a string called TooltipText . TooltipText default value is empty string \"\". It gets populated later on under some conditions.

I have bound the Tooltip property of a control in wpf to a string called TooltipText . TooltipText default value is empty string "". It gets populated later on under some conditions.

The problem is when the TooltipText is empty it looks odd when the user does mouse over my control as it displays an empty box tooltip.

Is there a way to NOT SHOW the tooltip when TooltipText is empty but show it when its length is greater than 1? I hope I made myself clear.

I 开发者_JAVA技巧do this in xaml as (code is incomplete and only partial):

<c:MyControl ToolTip="{Binding ElementName=controlName, Path=TooltipText}">


Set the property to null instead of "".


Excellent answer on this topic already. Works like a charm!

Copied the relevant code here for posterity.

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

Code behind:

private void TextBlock_IsMouseDirectlyOverChanged(object sender, 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消