views:

35

answers:

3
+2  Q: 

Tooltip Visibility

I have the following code:

<DataTemplate>
    <!--<sdk:DataGridTextColumn Binding="{Binding Description}" Header="Description" Width="205" />-->
    <TextBlock  Text="{Binding Description}"    Width="232">
        <ToolTipService.ToolTip  >   
            <ToolTip Visibility="{Binding    }">
                <sdk:DataGrid AutoGenerateColumns="False" GridLinesVisibility="None" HeadersVisibility="None" Height="Auto" ItemsSource="{Binding Contains}" >
                    <sdk:DataGrid.Columns>
                        <sdk:DataGridTextColumn Binding="{Binding Code}" Header="Code" CanUserSort="False" />
                        <sdk:DataGridTextColumn Binding="{Binding Description}" Header="Description" CanUserSort="False"/> 
                    </sdk:DataGrid.Columns>
                </sdk:DataGrid>            
            </ToolTip>
        </ToolTipService.ToolTip>
    </TextBlock>
</DataTemplate>

I would like my tooltip to show, only if there are at least one row in the ItemsSource="{Binding Contains}"

What do I have to write in the Visibility property? Something like

<ToolTip Visibility="{Binding Contains.Length > 0}">

But I can't figure out what the syntax should be! Any ideas?

+3  A: 

You are going to have to use a custom converter on that binding. Visibility property is not boolean, it is an enum.

Muad'Dib
+2  A: 

As an alternative way, you can use triggers: Set visibility of your tooltip to Visible by default and add this markup:

<DataTemplate.Triggers>
    <DataTrigger Binding="{Binding Contains.Length}" Value="0">
        <Setter TargetName="myTooltip" Property="Visibility" Value="Hidden" />
    </DataTrigger>
</DataTemplate.Triggers>
Nagg
I get The attachable property 'Triggers' was not found in type 'DataTemplate'what is missing?
bmanu
Oh, sorry. datatrigger is for wpf :(
Nagg
+1  A: 

This is a job for an implementation of IValueConverter. Having posted many such examples in the past I realised that a couple of more general implementations would work of most of the time, so I blogged them.

The IValueConverter you need is my StringToObjectConverter blogged here. With the code for this converter in your project you can create an instance of it in a resource like this:-

<Grid x:Name="LayoutRoot">
    <Grid.Resources>
        <local:StringToObjectConverter x:Key="CountToVisibility">
            <ResourceDictionary>
                <Visibility x:Key="__Default__">Visible</Visibility>
                <Visibility x:Key="0">Collapsed</Visibility>
            </ResourceDictionary>
        </local:StringToObjectConverter>
    </Grid.Resources>

and then used when binding to visibility, in you case:-

 <ToolTip Visibility="{Binding Contains.Length, Converter={StaticResource CountToVisibility}}">

BTW, are you sure you want Length not Count?

AnthonyWJones
Resharper says these 2 lines are never used: <Visibility x:Key="__Default__">Visible</Visibility> <Visibility x:Key="0">Collapsed</Visibility>
bmanu
@bmanu: I hear some say Resharper is an essential tool for .NET developers, funny that.
AnthonyWJones
but it doesn't work so far - Does the resharper comment help?
bmanu
@bmanu: Works fine, in my testing. Try testing your binding, remove the `Visiblity` and all the clever content from the ToolTip and just use this `<TextBlock Text="{Binding Contains.Length}" />`. Does the tooltip display the length value you are expecting?
AnthonyWJones
Yes it works very nicely - Sorry for the confusion, Resharper threw me off a little - Thanks Anthony!
bmanu