Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

New Post: DateTimePicker- Updating and displaying value from Code

$
0
0
Hi,

In XAML, for a DateTimePicker, you don't need to bind the Value and the Text property because when the Value property is modified, the Text property will automatically be updated. Same thing in code-behind.

Have a look at my sample :
<StackPanel>
      <xctk:DateTimePicker Name="dtp_StartDTPicker"
                           Value="{Binding Path=StartDT, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />

      <xctk:DateTimePicker Name="dtp_EndDTPicker"
                           Value="{Binding Path=EndDT}"/>

      <Button Content="TEST"
              Click="Button_Click_1" />
 </StackPanel>

public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      dtp_StartDTPicker.DataContext = this;
      dtp_EndDTPicker.DataContext = this;

      this.StartDT = new DateTime( 2015, 2, 10 );
      this.EndDT = new DateTime( 2015, 2, 20);     
      
    }

    #region StartDT

    public static readonly DependencyProperty StartDTProperty = DependencyProperty.Register( "StartDT", typeof( DateTime )
      , typeof( MainWindow ), new UIPropertyMetadata( null ) );
    public DateTime StartDT
    {
      get
      {
        return (DateTime)GetValue( StartDTProperty );
      }
      set
      {
        SetValue( StartDTProperty, value );
      }
    }

    #endregion //StartDT

    #region EndDT

    public static readonly DependencyProperty EndDTProperty = DependencyProperty.Register( "EndDT", typeof( DateTime )
      , typeof( MainWindow ), new UIPropertyMetadata( null ) );
    public DateTime EndDT
    {
      get
      {
        return (DateTime)GetValue( EndDTProperty );
      }
      set
      {
        SetValue( EndDTProperty, value );
      }
    }

    #endregion //EndDT

    private void Button_Click_1( object sender, RoutedEventArgs e )
    {
      dtp_StartDTPicker.Value = new DateTime( 2015, 3, 20 );
      dtp_EndDTPicker.Value = new DateTime( 2015, 3, 30 );

      System.Diagnostics.Debug.WriteLine( "Start Value = {0}", dtp_StartDTPicker.Value );
      System.Diagnostics.Debug.WriteLine( "Start Text = " +  dtp_StartDTPicker.Text );
      System.Diagnostics.Debug.WriteLine( "End Value = {0}", dtp_EndDTPicker.Value );
      System.Diagnostics.Debug.WriteLine( "End Text = " + dtp_EndDTPicker.Text );
    }
  }

Viewing all articles
Browse latest Browse all 4964

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>