开发者

DatePicker SelectionChanged Code is not working - WPF

Here is my code in WPF: XAML:

<StackPanel Width="150">
    <DatePicker Name="dpick" FirstDayOfWeek="Monday" SelectedDateFormat="Short"/>
    <TextBlock Name="dpText"/>
</StackPanel>

C# Code:

public void dpick_SelectionChanged(object sender, EventArgs e)
{
    dpText.Text = dpick开发者_如何学Python.SelectedDate.Value.Year.ToString() + "-" +
            dpick.SelectedDate.Value.Month.ToString() + "-" +
            dpick.SelectedDate.Value.Day.ToString();
}

dpText.Text is not updated after making a change in date. Why this is happening. I have tried with ValueChanged event also.Still no update is happening.


You are not using any of the events on the date picker. Try to add the SelectedDateChanged to the picker, and put your code from dpick_SelectionChanged in the newly created event instead.

<StackPanel Width="150">
    <DatePicker Name="dpick"  FirstDayOfWeek="Monday" SelectedDateFormat="Short"
                SelectedDateChanged="dpick_SelectedDateChanged"/>
    <TextBlock Name="dpText"/>
</StackPanel>

Code:

private void dpick_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
    dpText.Text = dpick.SelectedDate.Value.Year.ToString() + "-" +
        dpick.SelectedDate.Value.Month.ToString() + "-" +
        dpick.SelectedDate.Value.Day.ToString();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜