首页 > 代码库 > WPF DatePicker 添加水印效果
WPF DatePicker 添加水印效果
这个控件没有水印属性,依然使用依赖属性解决
public class DatePickerHelper { public static object GetWatermark(DependencyObject obj) { return (object)obj.GetValue(WatermarkProperty); } public static void SetWatermark(DependencyObject obj, object value) { obj.SetValue(WatermarkProperty, value); } // Using a DependencyProperty as the backing store for Watermark. This enables animation, styling, binding, etc... public static readonly DependencyProperty WatermarkProperty = DependencyProperty.RegisterAttached("Watermark", typeof(object), typeof(DatePickerHelper), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnWatermarkChanged))); private static void OnWatermarkChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var datePicker = d as DatePicker; if (datePicker == null) return; if (datePicker.IsLoaded) { SetWatermarkInternal(datePicker, e.NewValue); } else { RoutedEventHandler loadedHandler = null; loadedHandler = delegate { datePicker.Loaded -= loadedHandler; SetWatermarkInternal(datePicker, e.NewValue); }; datePicker.Loaded += loadedHandler; } } private static void SetWatermarkInternal(DatePicker d, object value) { var textBox = d.Template.FindName("PART_TextBox", d) as Control; if (textBox != null) { var watermarkControl = textBox.Template.FindName("PART_Watermark", textBox) as ContentControl; if (watermarkControl != null) watermarkControl.Content = value; } } }
使用:
<DatePicker helper:DatePickerHelper.Watermark="Start Date" />
WPF DatePicker 添加水印效果
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。