1

I am currently facing a small issue with my DataGridTextColumn.

I want to display a tooltip at a DataGridTextColumn but only if the text is not empty.

How can I achieve this? The code that I am currently using:

                <DataGridTextColumn IsReadOnly="True" Header="Person" Binding="{Binding SomeBinding, TargetNullValue='-'}" Width="Auto"
                CellStyle="{StaticResource SomeStyle}"/>

With the style

            <Style x:Key="SomeStyle" 
           TargetType="DataGridCell" BasedOn="{StaticResource InactiveStyle}">
        <Style.Setters>
            <Setter Property="ToolTip" Value="{Binding Path=SomeBinding}"/>
        </Style.Setters>
            </Style>

This code does provide me the tooltip, however, it is also showing the tooltip when there is no text. If there are any questions, please let me know and I can help you.

2 Answers 2

2

Try to add data triggers for string.Empty and null:

<Style x:Key="SomeStyle" TargetType="DataGridCell" 
       BasedOn="{StaticResource InactiveStyle}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=SomeBinding}" Value="">
            <Setter Property="ToolTip" Value="{x:Null}"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=SomeBinding}" Value="{x:Null}">
            <Setter Property="ToolTip" Value="{x:Null}"/>
        </DataTrigger>
    </Style.Triggers>
    <Style.Setters>
        <Setter Property="ToolTip" Value="{Binding Path=SomeBinding}"/>
    </Style.Setters>
</Style>
Sign up to request clarification or add additional context in comments.

Comments

1

Here is an approach :

    //Create a class which inherits from IValueConverter
    public class CellToolTipConverter : IValueConverter
    {
     #region IValueConverter Membres

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string stringValue = (string)value;
        if (!string.IsNullOrEmpty(stringValue))
            return  "Your tooltip";//As you are in a c# class, you have many possibilities.
       else
        return string.Empty;
    }

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

    #endregion
   }



     //In your xaml :

     //Declare your namespace
     xmlns:CustomClasses="clr-namespace:YourAssamblyName.YourNameSpaceOfConverterClass"


     <UserControl.Resources>
         <CustomClasses:CellToolTipConverter x:Key="CustomToolTipConverter"/>              
     </UserControl.Resources>



        //In your grid view
        <GridView.RowStyle>
            <Style TargetType="{x:Type telerik:GridViewRow}">
                <Setter Property="MyCustomToolTipProperty" Value="{Binding YourProperty, Converter= 
     {StaticResource CustomToolTipConverter}}"/>
            </Style>
        </GridView.RowStyle>

2 Comments

Thanks for your answer! I am currently using the answer from mm8.
No, problem, I agree, it can be a better solution, I will keep the mine in case you or another person needs. Because the converter class is really flexible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.