Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty tooltip issue

I have a simple WPF application with a button whose tooltip is binded to a TextBlock.Text:

<Grid>
    <Button Height="23" Margin="82,0,120,105" Name="button1" VerticalAlignment="Bottom" ToolTip="{Binding ElementName=textBlock1,Path=Text}" Click="button1_Click">Button</Button>
    <TextBlock Height="23" Margin="64,55,94,0" Name="textBlock1" VerticalAlignment="Top" Text="AD" />
</Grid>

In button1_Click, I have:

textBlock1.Text = null;

I expected no tooltip for the button after the button click. However, I am getting an emty tooltip. How do I fix this?

like image 757
Student_sjce Avatar asked Sep 08 '11 10:09

Student_sjce


1 Answers

You cannot set the Text to null, it will change to an empty string right away.

Workaround #1: a style that clears the value:

<!-- Do NOT set the ToolTip on the Button itself -->
<Style TargetType="Button" xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Setter  Property="ToolTip" Value="{Binding ...}"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding ...}" Value="{x:Static sys:String.Empty}">
            <Setter Property="ToolTip" Value="{x:Null}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Workaround #2: Add a ValueConverter to the binding which returns null if the value is String.Empty.

There might be other ways.

like image 74
H.B. Avatar answered Sep 23 '22 21:09

H.B.