DataGridTextColumn的条件工具提示



我的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>

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

尝试为string.Emptynull:添加数据触发器

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

这里有一种方法:

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

最新更新