开发者

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 approac开发者_StackOverflow社区h 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. =)


How about creating custom style for Tooltip ? That way you can re-use the same functionality at several places with minimal code.

Add this to a resourcedictionary and include it where ever you want to over-ride default tooltip behavior -

<Style TargetType="ToolTip" x:Key="{x:Type ToolTip}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Content, 
                                           RelativeSource={RelativeSource Self}, 
                                           Converter={local:ToolTipContentConverter}}" 
                         Value="">
                <Setter Property="Visibility" Value="Hidden"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

Followed by the converter -

 [ValueConversion(typeof(object), typeof(string))]
    public class ToolTipContentConverter : MarkupExtension, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value ?? string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

Hope this helps.. Amit


You should set the visibility on the grid :

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="visibilityConverter" />
</Window.Resources>


...

<Grid Visibility="{Binding IsToolTipVisible, Converter={StaticResource visibilityConverter}}>
...
</Grid>


If you don'e want the tooltip to show empty frame. You should create a separate tooltip ControlTemplate with all your required grid and textblocks and assign it to the tooltip template. This could help you solve the empty frame problem.


A much simpler solution than the other answers:

<TextBlock ToolTipService.IsEnabled="{Binding MakeToolTipVisible}">
    <TextBlock.ToolTip>
        <Grid>...</Grid>
    </TextBlock.ToolTip>
</TextBlock>

where MakeToolTipVisible is your property.


Example: I want to store information about the line and add to my canvas

Line line = new Line();
line.X1 = 100;
line.Y1 = 100;
line.X2 = 500;
line.Y2 = 100;
line.Stroke = Brushes.Red;
line.StrokeThickness = 1;
line.ToolTip = new ToolTip { Content = "This is a line",Visibility= Visibility.Collapsed };
canvas.Children.Add(line);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜