简体   繁体   中英

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:

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.

Note: I want my DatePicker to be set to a default value of my choosing which in this case is 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM