Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding conditional visibility to WPF control ToolTip

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.

like image 751
Sonic Soul Avatar asked Aug 18 '10 13:08

Sonic Soul


4 Answers

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>
like image 185
Crispy Avatar answered Oct 30 '22 11:10

Crispy


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
}
like image 32
Ryan Spears Avatar answered Oct 30 '22 12:10

Ryan Spears


Here you go;

   <TextBlock Text="Dummy text">
        <TextBlock.ToolTip>
            <ToolTip Visibility="Collapsed">
                <TextBlock Text="Text tooltip"></TextBlock>
            </ToolTip>                
        </TextBlock.ToolTip>
    </TextBlock>
like image 33
NetSide Avatar answered Oct 30 '22 11:10

NetSide


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. =)

like image 32
Yatrix Avatar answered Oct 30 '22 12:10

Yatrix