可以,方法有2种,一种是用代码,一种是重写样式,我来演示一下用代码怎么实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//首先在你的App类,即创建项目的时候App.xaml下的类
public partial class App : Application
{
//在启动方法中为时间控件注册一个加载事件
void app_Startup(object sender,StartupEventArgs e)
{
EventManager.RegisterClassHandler(typeof(DatePicker),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(DatePicker_Loaded));
}
void DatePicker_Loaded(object sender, RoutedEventArgs e)
{
var dp = sender as DatePicker;
if (dp == null) return;
var wm = RecursionFindControl
if (wm == null) return;
//重新默认水印值
wm.Content = "请选择一个日期";
}
///
/// 递归寻找指定名称和指定类型的控件(辅助方法)
///
///
/// 对象
/// 需要寻找的控件名称
/// 最大递归级数
/// 当前已经递归的级数
///
public static T RecursionFindControl
{
DependencyObject obj = null;
if (reference == null)
{
return obj as T;
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(reference); i++)
{
obj = VisualTreeHelper.GetChild(reference, i);
FrameworkElement elemnt = obj as FrameworkElement;
if (elemnt == null)
{
break;
}
if (elemnt is T &&
((!string.IsNullOrEmpty(controlName)
&& elemnt.Name == controlName)
|| string.IsNullOrEmpty(controlName)))
{
return obj as T;
}
else
{
if (currentLevel < maxLevel)//递归层级限制
{
obj = RecursionFindControl
if (obj != null)
{
return obj as T;
}
}
}
}
return obj as T;
}
}