WPF通过样式实现空值ToolTipe不显示

本文介绍了如何在WPF中通过样式设置,使得当TextBox内容为空时,Tooltip不显示。文章详细讲解了使用NullOrEmptyConverter转换器,并在Global.xaml和APP.xaml中注册样式,以及在MainWindow中实现绑定值的Tooltip验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NullOrEmptyConverter  类

    public class NullOrEmptyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return true;
            if (value is string && value.ToString().Trim() == string.Empty) return true;
            return false;
        }

        public object ConvertBack(object value, Type targetType, object paramter, CultureInfo culture)
        {
            throw new InvalidOperationException("IsNullConver can only be used oneWay.");
        }
    }


样式:Global.xaml

<ResourceDictionary 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:TextStyleResource="clr-namespace:WpfApplication1"              
      mc:Ignorable="d" 
                    
     >

    <TextStyleResource:NullOrEmptyConverter x:Key="nullOrEmptyConverter"/>

    <Style x:Key="Style_TextBox" TargetType="{x:Type TextBox}">
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Margin" Value="20,8,0,0"/>
    </Style>

    <Style x:Key="Stype_ReadOnlyTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource Style_TextBox}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self},Path=Text,Converter={StaticResource nullOrEmptyConverter}}" Value="False">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=Text}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

</ResourceDictionary>


APP.xmal注册样式

  <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Global.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>


MainWindow中对绑定的值实现ToolTipe验证,为空 不显示ToolTipe

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:loc="clr-namespace:WpfApplication1"  
        Name="win"
        Title="MainWindow" Height="350" Width="525">
        <Grid>
        <TextBox x:Name="TxtTextBox"  Width="200" Height="40" Style="{StaticResource Stype_ReadOnlyTextBox}"
                 Text="{Binding ElementName=win,Path=DsiplayStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
    </Grid>
</Window>

定义绑定的属性

 /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        public MainWindow()
        {
            InitializeComponent();
            this.DsiplayStr = "ABCDEFGH";
        }


        private string _dsiplayStr;

        public string DsiplayStr
        {
            get { return _dsiplayStr; }
            set {
                _dsiplayStr = value;

                PropertyChanged(this,new PropertyChangedEventArgs("DsiplayStr"));
            }
        }
        
    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值