[英]Why Text property of a DatePicker still contains value?
I am building a WPF/C# application and I have several DatePicker-s which I reference using this construct: 我正在构建一个WPF / C#应用程序,我使用此构造引用了几个DatePicker-s:
for(int i = 0; i < n; i += 1)
{
DatePicker dp = (DatePicker)this.FindName(Constants.DP_PRODUCT_PREFIX + (i + 1));
dp.SelectedDate = new DateTime(2017, 12, 31);
dp.IsEnabled = true;
dp.Opacity = 1;
dp.Text = "";
}
However even if I set 但是即使我设置
dp.Text = "",
the DatePickers still show their textfield with 31/12/2017. DatePickers仍在2017年12月31日显示其文本字段。
Note: I want my DatePicker to be set to a default value of my choosing which in this case is 31.12.2017 注意:我希望将DatePicker设置为我选择的默认值,在这种情况下为31.12.2017
What do I do wrong? 我做错了什么? Is it maybe the time when I run this code, it doesn't apply the change?
可能是我运行这段代码的时间,它不应用更改吗?
You can find DatePickerTextBox in DatePicker Template(which displays SelectedDate value) to assign new value.The default name for DatePickerTextBox in Template was "PART_TextBox" so you can find it from Template like this 您可以在DatePicker模板(显示SelectedDate值)中找到DatePickerTextBox来分配新值。Template中DatePickerTextBox的默认名称为“ PART_TextBox”,因此您可以从Template中找到它
dp.Loaded += (ee, ss) =>
{
var template = dp.Template;
var t = template.FindName("PART_TextBox", dp) as DatePickerTextBox;
if (t != null)
t.Text = "";
};
put the code in Loaded event to make sure DatePickerTextBox is not null.You can also see DatePicker Template by right click on the control then select edit style. 将代码放入Loaded事件中,以确保DatePickerTextBox不为null。您还可以通过右键单击控件然后选择编辑样式来查看DatePicker模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.