Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPFC#4.0
I have a WPF application, with a WPF Toolkit DatePicker control bound to a DateTime variable as following:
 
<myToolkit:DatePicker Grid.Row="4" Grid.Column="4" x:Name="clndrHiredate" HorizontalAlignment="Left" Height="20" Width="150" VerticalAlignment="Top" FontFamily="Tahoma" FontWeight="Light" FlowDirection="RightToLeft" SelectedDate="{Binding bindingDate,StringFormat='{}{0:MM/dd/yyyy}', Mode=TwoWay}" />
 
<Button Grid.Row="8" Grid.Column="4" Style="{StaticResource GlassButton}" x:Name="btnRead" Height="29" Content="Read" FontSize="14" Foreground="White" HorizontalContentAlignment="Center" FontFamily="Arial" FontWeight="Bold" Background="#FF47594E" Margin="64,13,31,196" Width="65" />
And my code :
private DateTime bindingDate = DateTime.Now;
 
private void btnRead_Click(object sender, RoutedEventArgs e)
{
      if (string.IsNullOrEmpty(clndrHiredate.SelectedDate.ToString()))
      {
          errorText += MessageBox.show("Wrong");
          state = true;
          lblHireDate.Visibility = Visibility.Visible;
      }
      else
      {
          // complete 
      }
}
but when I run this , I got error message "string was not recognized as a valid datetime."
 
If there is any error, How do I fix this?
 
Thanks.
 
[edit]fixed code tags[/edit]
Posted 10-Nov-12 10:12am
Edited 10-Nov-12 10:28am
Nelek42.1K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I solved this like that:
 
<mytoolkit:datepicker grid.row="4" grid.column="4" x:name="clndrHiredate" horizontalalignment="Left" height="20" xmlns:x="#unknown" xmlns:mytoolkit="#unknown">
                  Width="150" VerticalAlignment="Top" FontFamily="Tahoma" FontWeight="Light" FlowDirection="RightToLeft"/>
 
private void Window_Loaded(object sender, RoutedEventArgs e)
   {
   this.clndrHiredate.SelectedDate = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy"));
   }
 
private void btnRead_Click(object sender, RoutedEventArgs e)
{
  if (clndrHiredate.SelectedDate == null)
     errorText += MessageBox.show("Wrong");
          state = true;
          lblHireDate.Visibility = Visibility.Visible;
      }
      else
      {
          // complete 
      }
 
}</mytoolkit:datepicker>
[edit]code block added[/edit]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can not bind to private field; you need public property (notifying property or a dependency property) to bind to it. Here is my suggestion and I hope it may help you
 
Declare new dependency property in the Window class like this:
 
public DateTime BindingDate
{
    get { return (DateTime)GetValue(DateBindingProperty); }
    set { SetValue(DateBindingProperty, value); }
}
 
public static readonly DependencyProperty DateBindingProperty =
    DependencyProperty.Register("DateBinding", typeof(DateTime), typeof(MainWindow), new FrameworkPropertyMetadata(DateTime.Now));
 

Then replace your XAML code to bind the SelectedDate to this dependency property (BindingDate) like this:
 
        <DatePicker Height="29" HorizontalAlignment="Left" 
                    Margin="78,65,0,0" Name="datePicker1" VerticalAlignment="Top" Width="163">
            <DatePicker.SelectedDate>
                <Binding Path="BindingDate" Mode="TwoWay">
                    <Binding.RelativeSource>
                            <RelativeSource
                                Mode="FindAncestor"
                                AncestorType="{x:Type Window}"
                                AncestorLevel="1"/>
                        </Binding.RelativeSource>
                </Binding>
            </DatePicker.SelectedDate>
        </DatePicker>
  Permalink  
v2
Comments
Doudy_2020 at 11-Nov-12 4:00am
   
Thanks Issam but I still have same error
Issam Ali at 11-Nov-12 5:55am
   
did you use the same code above -the one in my solution- as it?
Doudy_2020 at 11-Nov-12 6:11am
   
Yes, I did
Issam Ali at 11-Nov-12 6:29am
   
did the DatePicker get the (Now) value on application start?
Doudy_2020 at 11-Nov-12 6:57am
   
Yes, when application start datepicker show the date of today

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 442
1 sanket saxena 355
2 Abhinav S 268
3 OriginalGriff 250
4 CPallini 205
0 Sergey Alexandrovich Kryukov 8,704
1 OriginalGriff 5,000
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 11 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid