Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WPF application, with a WPF Toolkit DatePicker control bound to a DateTime variable as following:

C#
<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 :
C#
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
Updated 10-Nov-12 10:28am
v2

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:

C#
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>
 
Share this answer
 
v2
Comments
Doudy_2020 11-Nov-12 4:00am    
Thanks Issam but I still have same error
Issam Ali 11-Nov-12 5:55am    
did you use the same code above -the one in my solution- as it?
Doudy_2020 11-Nov-12 6:11am    
Yes, I did
Issam Ali 11-Nov-12 6:29am    
did the DatePicker get the (Now) value on application start?
Doudy_2020 11-Nov-12 6:57am    
Yes, when application start datepicker show the date of today
I solved this like that:

C#
<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]
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900