wpf tootip

自定义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="测试文本&#x000A;测试文本" 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值