Blank tooltip

I have a simple WPF application with a button whose tooltip is bound to 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 did not expect a tooltip for the button after clicking the button. However, I get an emty tooltip. How to fix it?

+5
source share
1 answer

You cannot set the Textvalue to null, it will immediately change to an empty string.

Workaround # 1: a style that clears the meaning:

<!-- 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 ValueConverterto the binding, which returns nullif valueis String.Empty.

There may be other ways.

+5

All Articles