|
概述:WPF中通过`Style`实现TextBox水印文本,使用`WatermarkTextBox`类及`ControlTemplate`。这个示例通过`VisualStateManager`在文本框失去焦点且内容为空时显示水印文本。通过`Watermark`属性简化水印文本设置,提高可维护性。
在WPF中,通过Style实现TextBox中的水印文本(水印、提示、占位符文本)通常使用ControlTemplate和VisualStateManager。以下是一个详细的实例源代码:XAML文件:- <Window x:
- <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary>xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary>xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary>xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary>xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary>mc:Ignorable="d"
- <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary>Title="MainWindow" Height="350" Width="525">
- <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary><Grid>
- <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary><local:WatermarkTextBox Watermark="请输入内容" Width="200" Height="30"/>
- <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary></Grid>
- </Window>
复制代码 Themes/Generic.xaml文件:- <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfWatermarkExample">
-
- </ResourceDictionary>
复制代码 看效果:
这个例子中,自定义了WatermarkTextBox类,其中包含一个Watermark属性。在Themes/Generic.xaml中,定义了WatermarkTextBox的ControlTemplate,通过VisualStateManager在未获得焦点且文本为空时显示水印文本。在MainWindow.xaml中使用了WatermarkTextBox并设置了水印文本。
源代码获取:https://pan.baidu.com/s/1YCuKZhEOrs-C3nGqjNB-lA?pwd=6666
来源:https://www.cnblogs.com/hanbing81868164/p/18103345
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|