DatePicker on two different fields how to keep date input by user?

Dimitri Garmaise 266 Reputation points

Hello, I have two DatePicker fields in XAML:
<DatePicker x:Name="DtpCurrencyExchangeDate0" HorizontalAlignment="Left" Margin="237,134,0,0" VerticalAlignment="Top" Text="{Binding Path=CurrencyExchangeDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat={0:d}, ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}" FirstDayOfWeek="Monday" SelectedDate="{Binding DisplayDate.Today, ElementName=DtpCurrencyExchangeDate0, Mode=OneWay}" IsTodayHighlighted="True" Height="25"/>

<DatePicker x:Name="DtpCurrencyExchangePrevDate0" HorizontalAlignment="Left" Margin="239,189,0,0" VerticalAlignment="Top" Text="{Binding Path=CurrencyExchangePrevDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat={0:d}, ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}" FirstDayOfWeek="Monday" SelectedDate="{Binding DisplayDate.Today, ElementName=DtpCurrencyExchangePrevDate0, Mode=OneWay}" IsTodayHighlighted="True" Height="25"/>

They both propose the current date to the user and its ok.
The user wants to change the proposed dates making the input of the new dates directly in the two fields (the dates are correct) but it seems that when the field loose the focus it comes back to the initial proposed date. I want to keep the new values but I don't find the right way to update the SelectedDate with the new value... Do we have a parameter in XAML to force that update or is it necessary to manage it with event and code behind? Can someone help me ? Thanks in advance.

A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
645 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 17,381 Reputation points Microsoft Vendor

    You could try to refer to the following code.


      <DatePicker x:Name="DtpCurrencyExchangeDate0" HorizontalAlignment="Left"  VerticalAlignment="Top"   
                         Text="{Binding Path=SelectedDate, ElementName=DtpCurrencyExchangeDate0, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ConverterCulture={x:Static gl:CultureInfo.CurrentCulture} }"  
                        FirstDayOfWeek="Monday" SelectedDate="{Binding CurrencyExchangeDate, Mode=TwoWay}"  
                        IsTodayHighlighted="True" Height="25"/>  
            <DatePicker x:Name="DtpCurrencyExchangePrevDate0" HorizontalAlignment="Left" VerticalAlignment="Top"   
                         Text="{Binding Path= SelectedDate, ElementName=DtpCurrencyExchangePrevDate0, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}"  
                        FirstDayOfWeek="Monday" SelectedDate="{Binding CurrencyExchangePrevDate,Mode=TwoWay}"  
                        IsTodayHighlighted="True" Height="25"/>  


    using System;  
    using System.ComponentModel;  
    using System.Globalization;  
    using System.Windows;  
    using System.Windows.Markup;  
    namespace DataPickerInput  
        public partial class MainWindow : Window,INotifyPropertyChanged  
            public MainWindow()  
                DataContext = this;  
                this.Language = XmlLanguage.GetLanguage(  
            private DateTime currencyExchangePrevDate = DateTime.Now ;  
            public DateTime CurrencyExchangePrevDate  
                get { return currencyExchangePrevDate; }  
                    currencyExchangePrevDate = value;  
            private DateTime currencyExchangeDate = DateTime.Now;  
            public DateTime CurrencyExchangeDate  
                get { return currencyExchangeDate; }  
                    currencyExchangeDate = value;  
            public event PropertyChangedEventHandler PropertyChanged;  
            private void OnPropertyChanged(String propertyName)  
                PropertyChangedEventHandler handler = PropertyChanged;  
                if (handler != null)  
                    handler(this, new PropertyChangedEventArgs(propertyName));  

    The result:


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful