Vinod
Vinod

Reputation: 4352

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.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.

Upvotes: 1

Views: 17084

Answers (1)

Bassetassen
Bassetassen

Reputation: 21784

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();
}

Upvotes: 10

Related Questions