简体   繁体   English

为什么DatePicker的Text属性仍然包含值?

[英]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.

相关问题 Linq查询-列表上的“哪里” <T> 其中反映的属性包含文本/值 - Linq query - “Where” on List<T> Where reflected property Contains text/value DatePicker之后TextBox.Text的值错误 - Wrong value with TextBox.Text after DatePicker DatePicker 删除文本后不更新值 - DatePicker not updating value after deleting text WPF绑定:属性包含值的路径 - WPF Binding: Where a property contains the path to the value 从对象获取属性并检查其是否包含值 - Get property from object and check if it contains value 检查对象列表是否包含具有特定值的属性 - Checking if a list of objects contains a property with a specific value 检查 JSON 属性是否包含值或数组 - Check whether JSON property contains value or array 数据绑定:“ DatePickerControl.DatePicker”不包含名称为“ Text”的属性 - DataBinding: 'DatePickerControl.DatePicker' does not contain a property with the name 'Text' 当包含值时,为什么Contains(value)等于false? - Why does the Contains(value) equal false when it contains the value? 为什么 JavaScript 运行时错误:Object 不支持属性或方法 'datepicker' 发生?(datepicker 不是函数) - Why does JavaScript runtime error: Object doesn't support property or method 'datepicker' happen?(datepicker is not a function)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM