问题
<DataGrid x:Name="dgRecords1"
CanUserAddRows="False" IsReadOnly="True"
ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}"
Style="{DynamicResource StyleDatagrid}"
SelectionChanged="dgRecords1_SelectionChanged"
Height="251" Width="569" Margin="41,173,168,0">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="ToolTip">
<Setter.Value>
<Border Width="200" Height="80"
BorderBrush="Black" BorderThickness="1"
Background="AliceBlue">
<StackPanel Orientation="Vertical">
<StackPanel Height="30" Background="Black">
<TextBlock Text="Email Sent To"
FontSize="14" FontWeight="Bold" Foreground="White"/>
</StackPanel>
<StackPanel>
<TextBlock Text="{Binding SentToList}"
TextWrapping="Wrap" FontWeight="Bold"/>
</StackPanel>
</StackPanel>
</Border>
</Setter.Value>
</Setter>
</Style>
</DataGrid.RowStyle>
In the code above,
<TextBlock TextWrapping="Wrap" FontWeight="Bold" Text="{Binding SentToList}" />
I want to check whether there is something in this textblock, and if there is nothing, I need to make the tooltip invisible. Is there some way of doing it using Triggers?
回答1:
You could surround your Border by a ToolTip control and bind the Visibility of that control to the same SentToList property by using a binding converter that converts from string to Visibility.
<Style TargetType="DataGridRow">
<Style.Resources>
<local:StringToVisibilityConverter x:Key="StringToVisibilityConverter"/>
</Style.Resources>
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip Visibility="{Binding SentToList, Converter={StaticResource StringToVisibilityConverter}}">
<Border>
...
</Border>
</ToolTip>
</Setter.Value>
</Setter>
</Style>
The converter might look like this:
public class StringToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.IsNullOrWhiteSpace(value as string) ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
来源:https://stackoverflow.com/questions/13178134/how-to-bind-the-visibility-of-tooltip-to-the-text-in-textblock