Empty tooltip issue
I have a simple WPF application with a 开发者_Python百科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?
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.
精彩评论