Click here to Skip to main content
15,892,697 members

Response to: I got this error string was not recognized as a valid datetime when I validate WPF DatePicker control

Revision 2
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>
Posted 10-Nov-12 12:30pm by Issam Ali.
Tags: , ,
  Print Answers RSS
Top Experts
Last 24hrsThis month