views:

21

answers:

1

I have a usercontrol consisting of two DateTimePicker This component must be reusable and should expose properties which define, for example, the Visibility of elements, the WIDTH of the container, etc.. And of course, I'm able to retrieve the values entered.

So I created adequate DependencyProperty

        public DateTime StartDateValue
    {
        get { return (DateTime)GetValue(StartDateValueProperty); }
        set { SetValue(StartDateValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for StartDateValue.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty StartDateValueProperty =
        DependencyProperty.Register("StartDateValue", typeof(DateTime), typeof(HSWDateTimePicker), new PropertyMetadata(HSWDateTimePicker.StartDateValueChanged));

When I use my UserControl into a view, I do not know how to get in my viewmodel, the values entered in the DatePicker of my usercontrol.

My userControl:

<my:myDateTimePicker StartDateValue="{Binding StartDateDebut, Mode=TwoWay}" x:Name="myPeriod" />

And my Button:

<Button Content="Ok" Width="75" Height="20" Command="{Binding Path=SaveCommand, Mode=OneTime}"/>

Thanks, PM

A: 

Two things to check out. Make sure the datacontext of your usercontrol is your viewmodel.

Make sure that the property you bind to in the viewmodel implements INotifyPropertyChanged. Otherwise changes in the viewmodel will not update your user control.

Make sure on the changed event of your Dependancy property that you set the value of your datetime picker.

zachary
Thank you for that answer, but I do not want the DataContext of my usercontrol target a particular VIEWMODEL.Because my UserControl must be reusable.I actually found the solution with this link:http://stackoverflow.com/questions/1526767/silverlight-usercontrol-custom-property-binding
Zorg