i would like to make a textblock tooltip conditionally visible.
i have the tooltip defined as:
<TextBlock>
<TextBlock.ToolTip>
<Grid>...</Grid>
</TextBlock.ToolTip>
</TextBlock>
where would visibility property go in that definition? it doesn't seem to like any of my guesses.
there are some suggestions of just working with grid visibility. the problem with that approach is making the grid invisible will still show empty tooltip box.. which is why i am trying to control tooltip visibility.
Try this. It won't leave an empty frame.
<TextBlock Text="test">
<TextBlock.ToolTip>
<ToolTip Visibility="Visible">
Hello
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
<TextBlock Text="test">
<TextBlock.ToolTip>
<ToolTip Visibility="Hidden">
Hello
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
The TextBlock with its ToolTip:
<TextBlock Text="{Binding Path=TextBoxText}">
<TextBlock.ToolTip>
<ToolTip Visibility="{Binding Path=ToolTipText, Converter={StaticResource StringToVisibilityConverter}}">
<Grid><TextBlock Text="{Binding Path=ToolTipText}" /></Grid>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
The object to are binding to the TextBlock:
public class TextBoxBindingObject
{
public string TextBoxText{ get; set; }
public string ToolTipText{ get; set; }
}
The converter:
[ValueConversion(typeof(string), typeof(Visibility))]
public class StringToVisibilityConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if(value is string)
{
var stringValue = value as string;
if(!String.IsNullOrEmpty(stringValue))
return Visibility.Visible;
}
return Visibility.Hidden;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
Here you go;
<TextBlock Text="Dummy text">
<TextBlock.ToolTip>
<ToolTip Visibility="Collapsed">
<TextBlock Text="Text tooltip"></TextBlock>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
I realize this is a year old, but you can accomplish this in the code-behind.
private void control_ToolTipOpening(object sender, ToolTipEventArgs e)
{
if (condition)
e.Handled = true;
}
If you wanted to set a property here, you could do that, and bind it to the visibility. Don't forget to implement the INotifyPropertyChanged interface on your window.
Something like:
private void control_ToolTipOpening(object sender, ToolTipEventArgs e)
{
if (condition)
{
showControl=true;
e.Handled = true;
}
}
public Visibility showControl
{
get
{
return _showControl;
}
set
{
_showControl = value;
NotifyPropertyChanged("showControl");
}
}
and then bind it to the visibility property as
Visibility = "{Binding showControl}"
I'm typing this mainly to help anyone that comes across this from this point forward. I'm guessing you're not still stuck on this a year later, OP. =)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With