自定义tooltip,左上角有一个三角。支持文本换行。
<LH:NullToVisibilityConverter x:Key="nullToVisibilityConverter"/>
<Style TargetType="ToolTip">
<Setter Property="Visibility" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content, Converter={StaticResource nullToVisibilityConverter}}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<Path HorizontalAlignment="Left" VerticalAlignment="Top" Data="M 6 0 L 0 6 L 12 6 Z" Fill="#E6333333" Margin="0,0,10,0"/>
<Border CornerRadius="2" Background="{StaticResource RegionBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock Text="{TemplateBinding ContentControl.Content}" Foreground="White" TextWrapping="WrapWithOverflow" Margin="6"/>
</Border>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="FontSize" Value="14"/>
<Setter Property="Padding" Value="6,2,6,2"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
<TextBlock Text="{Binding ToolTip}" TextWrapping="Wrap" Width="100"/>
</ToolTip>
</Setter.Value>
</Setter>
</Style>
convert 实现,如果没有 text则自动隐藏 tooltips窗口
public class NullToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is string)
return value == null ? Visibility.Collapsed : Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
使用:
<StackPanel Grid.Row="0" Margin="5" Orientation="Horizontal" VerticalAlignment="Top">
<LH:SpinBoxControl ToolTip="测试文本
测试文本" SpinVisible ="False" Margin="10" LH:AttachedProperties.AttachString="4 1" ValueChanged="SBValueChanged" Value="{Binding AnalogGain}" Minimum="1.00" Maximum="4.00" Increment="0.01" Width="100" VerticalAlignment="Center">
<LH:SpinBoxControl.ValueArray>
<x:Array Type="{x:Type sys:Double}">
<sys:Double>1</sys:Double>
<sys:Double>5</sys:Double>
<sys:Double>8</sys:Double>
</x:Array>
</LH:SpinBoxControl.ValueArray>
</LH:SpinBoxControl>
<LH:SpinBoxControl Margin="10" LH:AttachedProperties.AttachString="4 2" ValueChanged="SBValueChanged" Value="{Binding DigitalGain}" ValueArrayString="1,2.1,3.11,5.11,102.11" Minimum="1.00" Maximum="8.00" Increment="0.01" Width="100" VerticalAlignment="Center"></LH:SpinBoxControl>
</StackPanel>