WPF——怎么让DatePicker什么都不显示为空白
如题。还有我怎么才能让用户一点击DatePicker的编辑区就让它的日期展开!谢谢啦。急!!!!...
如题。还有我怎么才能让用户一点击DatePicker的编辑区就让它的日期展开!谢谢啦。急!!!!
展开
展开全部
方法1:修改DatePicker的Template,里面有一个PART_TextBox,再修改它的Template,去掉里面的一个PART_Watermark,或者将它的Visiibility设置为Collapsed
方法2:你也可以做一个附加属性,这样可以任意修改里面的文字:
public static class DatePickerHelper
{
public static readonly DependencyProperty WatermarkProperty =
DependencyProperty.RegisterAttached("Watermark", typeof(object), typeof(DatePickerHelper),
new FrameworkPropertyMetadata(null,
new PropertyChangedCallback(OnWatermarkChanged)));
public static object GetWatermark(DatePicker d)
{
return (object)d.GetValue(WatermarkProperty);
}
public static void SetWatermark(DatePicker d, object value)
{
d.SetValue(WatermarkProperty, value);
}
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的默认样式里面,写上 <Setter Property="xxx:DatePickerHelper.Watermark" Value="" />即可
xxx是你引用DatePickerHelper的命名空间
至于第二个问题,跟上面的附加属性类似,你找到textBox之后,用
textBox.GotFocus += TextBoxOnGotFocus;
添加一个事件监听,在监听代码里面:
private static void TextBoxOnGotFocus(object sender, RoutedEventArgs e)
{
var textBox = sender as TextBox;
if (textBox != null)
{
var datePicker = textBox.TemplatedParent as DatePicker;
if (datePicker != null)
{
// 必须放到Input优先级执行,否则焦点会被DatePicker内部的逻辑抢走,导致异常
datePicker.Dispatcher.BeginInvoke(
new Action(() => datePicker.IsDropDownOpen = true), DispatcherPriority.Input);
}
}
}
方法2:你也可以做一个附加属性,这样可以任意修改里面的文字:
public static class DatePickerHelper
{
public static readonly DependencyProperty WatermarkProperty =
DependencyProperty.RegisterAttached("Watermark", typeof(object), typeof(DatePickerHelper),
new FrameworkPropertyMetadata(null,
new PropertyChangedCallback(OnWatermarkChanged)));
public static object GetWatermark(DatePicker d)
{
return (object)d.GetValue(WatermarkProperty);
}
public static void SetWatermark(DatePicker d, object value)
{
d.SetValue(WatermarkProperty, value);
}
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的默认样式里面,写上 <Setter Property="xxx:DatePickerHelper.Watermark" Value="" />即可
xxx是你引用DatePickerHelper的命名空间
至于第二个问题,跟上面的附加属性类似,你找到textBox之后,用
textBox.GotFocus += TextBoxOnGotFocus;
添加一个事件监听,在监听代码里面:
private static void TextBoxOnGotFocus(object sender, RoutedEventArgs e)
{
var textBox = sender as TextBox;
if (textBox != null)
{
var datePicker = textBox.TemplatedParent as DatePicker;
if (datePicker != null)
{
// 必须放到Input优先级执行,否则焦点会被DatePicker内部的逻辑抢走,导致异常
datePicker.Dispatcher.BeginInvoke(
new Action(() => datePicker.IsDropDownOpen = true), DispatcherPriority.Input);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询