【问题标题】:Conditional tooltip for DataGridTextColumnDataGridTextColumn 的条件工具提示
【发布时间】:2020-07-18 13:36:44
【问题描述】:

我的 DataGridTextColumn 目前遇到一个小问题。

我想在 DataGridTextColumn 上显示工具提示,但前提是文本不为空。

我怎样才能做到这一点?我目前使用的代码:

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

搭配风格

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

此代码确实为我提供了工具提示,但是,当没有文本时,它也会显示工具提示。如果有任何问题,请告诉我,我可以为您提供帮助。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    尝试为string.Empty和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>
    

    【讨论】:

      【解决方案2】:

      这是一种方法:

          //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>
      

      【讨论】:

      • 感谢您的回答!我目前正在使用 mm8 的答案。
      • 不,问题,我同意,这可能是一个更好的解决方案,我会保留我的,以防您或其他人需要。因为转换器类非常灵活。
      猜你喜欢
      • 2011-06-17
      • 2019-02-02
      • 2014-11-23
      • 2021-12-13
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 2019-01-01
      相关资源
      最近更新 更多