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

Dmtr_Grms 331 Reputation points
2022-11-10T17:07:09.36+00:00

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"/>

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

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

Accepted answer
  1. Hui Liu-MSFT 38,251 Reputation points Microsoft Vendor
    2022-11-14T06:44:09.327+00:00

    You could try to refer to the following code.

    Xaml:

      <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"/>  
    

    Codebehind:

    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()  
            {  
                InitializeComponent();  
                DataContext = this;  
                this.Language = XmlLanguage.GetLanguage(  
                           CultureInfo.CurrentCulture.IetfLanguageTag);  
            }  
            private DateTime currencyExchangePrevDate = DateTime.Now ;  
            public DateTime CurrencyExchangePrevDate  
            {  
                get { return currencyExchangePrevDate; }  
                set  
                {  
                    currencyExchangePrevDate = value;  
                    OnPropertyChanged("CurrencyExchangePrevDate");  
                     
                }  
            }  
            private DateTime currencyExchangeDate = DateTime.Now;  
            public DateTime CurrencyExchangeDate  
            {  
                get { return currencyExchangeDate; }  
                set  
                {  
                    currencyExchangeDate = value;  
                    
                    OnPropertyChanged("CurrencyExchangeDate");  
                }  
            }  
            public event PropertyChangedEventHandler PropertyChanged;  
            private void OnPropertyChanged(String propertyName)  
            {  
                PropertyChangedEventHandler handler = PropertyChanged;  
                if (handler != null)  
                {  
                    handler(this, new PropertyChangedEventArgs(propertyName));  
                }  
            }  
        }  
    }  
    

    The result:
    259947-5.gif

    ----------------------------------------------------------------------------

    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