我正在使用最新的WPF工具包,具体来说是DatePicker
。 一切正常,但是在没有提供值时,日期选择框中会出现默认的“SHOW CALENDAR”文本。
我想要在WPF中能够更改这个值。
有人告诉我下载源代码,添加一个新的依赖属性并重新编译成dll。那很棒,但如果发布新版本怎么办?
这就是为什么我想以这种方式对此控件进行模板化,以便我能够覆盖此默认字符串。 有任何想法吗?
谢谢!
我正在使用最新的WPF工具包,具体来说是DatePicker
。 一切正常,但是在没有提供值时,日期选择框中会出现默认的“SHOW CALENDAR”文本。
我想要在WPF中能够更改这个值。
有人告诉我下载源代码,添加一个新的依赖属性并重新编译成dll。那很棒,但如果发布新版本怎么办?
这就是为什么我想以这种方式对此控件进行模板化,以便我能够覆盖此默认字符串。 有任何想法吗?
谢谢!
好的,我已经自己找到了解决方案。
<Style TargetType="{x:Type toolkit:DatePickerTextBox}">
<Setter Property="Text" Value="Bitte wählen" />
</Style>
无论如何,你必须意识到事实上有一个名为Watermark的DependencyProperty应该代替Text进行设置。问题是,最新的MS版本(约在2009年6月左右)出于某种未知的原因将此属性设置为只读。这意味着,虽然会出现一次First-time异常(因为DatePicker试图解析字符串,他认为文本是日期),但通常你不会注意到它,这是我想出来的唯一hack。另一个可能性是直接编辑来自MS的源代码并覆盖SetWaterMark()方法+添加自己的Dependency Property(MyWaterMark或其他内容)。但那样你就不能使用提供的dll了。他们说它将在.NET 4发布时修复,让我们拭目以待。 void _datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
if (_datePicker1.SelectedDate != null) return;
FieldInfo fiTextBox = typeof(DatePicker)
.GetField("_textBox", BindingFlags.Instance | BindingFlags.NonPublic);
if (fiTextBox != null)
{
DatePickerTextBox dateTextBox =
(DatePickerTextBox)fiTextBox.GetValue(_datePicker1);
if (dateTextBox != null)
{
PropertyInfo piWatermark = dateTextBox.GetType()
.GetProperty("Watermark", BindingFlags.Instance | BindingFlags.NonPublic);
if (piWatermark != null)
{
piWatermark.SetValue(dateTextBox, "", null);
}
}
}
}
这个很好用,但是你还需要在自定义类中重写onrender方法。 在这个方法中,如果你设置了水印内容而不是属性,那么就没有必要重写OnSelectedDateChanged事件。 完整的代码在这里:
public string Watermark { get; set; }
protected override void OnSelectedDateChanged(SelectionChangedEventArgs e)
{
base.OnSelectedDateChanged(e);
//SetWatermark();
}
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
base.OnRender(drawingContext);
SetWatermark();
}
private void SetWatermark()
{
FieldInfo fiTextBox = typeof(DatePicker).GetField("_textBox", BindingFlags.Instance | BindingFlags.NonPublic);
if (fiTextBox != null)
{
DatePickerTextBox dateTextBox = (DatePickerTextBox)fiTextBox.GetValue(this);
if (dateTextBox != null)
{
if (string.IsNullOrWhiteSpace(this.Watermark))
{
this.Watermark = "Custom select a date";
}
//PropertyInfo piWatermark = typeof(DatePickerTextBox).GetProperty("Watermark", BindingFlags.Instance | BindingFlags.NonPublic);
//if (piWatermark != null)
//{
// piWatermark.SetValue(dateTextBox, this.Watermark, null);
//}
var partWatermark = dateTextBox.Template.FindName("PART_Watermark", dateTextBox) as ContentControl;
if (partWatermark != null)
{
partWatermark.Foreground = new SolidColorBrush(Colors.Gray);
partWatermark.Content = this.Watermark;
}
}
}
}
您也可以这样做:
在表单中添加文本框和日期选择器,并进行以下设置:
在window.xaml文件中:
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="22" Margin="38,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<DatePicker x:Name="datePicker" HorizontalAlignment="Left" Margin="130,37,0,0" VerticalAlignment="Top" Width="28" BorderBrush="Transparent" SelectedDateChanged="datePicker_SelectedDateChanged"/>
在你的 window.xaml.cs 文件中:
private void datePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
textBox.Text = datePicker.SelectedDate.Value.ToString("dd.MM.yyyy");
}
结果看起来像这样:点击