我有以下代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="{x:Type ToolTip}" TargetType="{x:Type ToolTip}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToolTip}">
<Border Background="Black">
<TextBlock FontFamily="Tahoma" FontSize="11" Text="{TemplateBinding Content}" Foreground="WhiteSmoke" Padding="2" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel>
<Label Content="Label 1" ToolTip="asd" />
<Label Content="Label 2" ToolTip="" />
<TextBlock Text="TextBlock 1" ToolTip="asd" />
<TextBlock Text="TextBlock 2" ToolTip="" />
<Button Content="Button 1" ToolTip="asd" />
<Button Content="Button 2" ToolTip="" />
</StackPanel>
现在,您可以通过测试看到当您将鼠标悬停在标签2,文本块2,按钮2上时,工具提示仍会显示。 我希望触发它,如果Tooltip为空或null,则它不应显示任何内容。我知道我可以简单地从XAML中移除它但是我在这里做的事情是不同的。
我尝试添加一个触发器来检查value =“”以及null和内部触发器,将模板设置为null但它们都没有工作
如果你们中的一些专家能够对此有所了解,我将非常高兴
答案 0 :(得分:13)
当DataTrigger可以完成同样的事情时,转换器可能过度。以下样式是您的发布样式,有一些清理和必要的触发器。请注意以下事项:
Visibility=Visible
。<强> XAML 强>
<Style TargetType="ToolTip">
<Setter Property="Visibility" Value="Visible" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Border Background="Black">
<TextBlock FontFamily="Tahoma"
FontSize="11"
Foreground="WhiteSmoke"
Padding="2"
Text="{TemplateBinding Content}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content}" Value="">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
答案 1 :(得分:6)
您可以考虑使用IValueConverter来显示/隐藏工具提示边框。将此类添加到项目中:
class BorderVisibilitySetter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//check if the control's content property is null or empty
if(value == null || value.ToString() == string.Empty)
return Visibility.Collapsed;
else
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后将您的xaml修改为:
<src:BorderVisibilitySetter x:Key="BorderVisible" />
<Style x:Key="{x:Type ToolTip}" TargetType="{x:Type ToolTip}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToolTip}">
<Border Background="Black" Visibility="{TemplateBinding Content, Converter={StaticResource BorderVisible}}" >
<TextBlock FontFamily="Tahoma" FontSize="11" Text="{TemplateBinding Content}" Foreground="WhiteSmoke" Padding="2" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 2 :(得分:0)
我的解决方案将具有以下功能:
Xaml代码:
<TextBlock Name="TxtBlockLicenseInfo" Height="35" ToolTipService.ShowDuration="200000" TextTrimming="CharacterEllipsis">
<TextBlock.ToolTip>
<ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}"
Visibility="{Binding Text,Converter={StaticResource TextToTooltipVisibilityConverter}}" >
<StackPanel>
<Label Content="{Binding Text}" Foreground="{Binding Foreground}" >
</Label>
</StackPanel>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
还在Xaml资源中包含转换器
<UserControl.Resources>
<local:LicenseInformationToTooltipVisibilityConverter x:Key="LicenseInformationToTooltipVisibilityConverter" />
</UserControl.Resources>
用于编写转换器的Xaml.Cs代码
public class TextToTooltipVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((value.ToString().Equals(string.Empty)))
{
return Visibility.Collapsed;
}
else return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Visibility.Visible;
}
}