WPF-MVVM Binding ViewModel-Property for Nested UserControl

As the title says, I want to bind a property from my ViewModel to a nested UserControl in the corresponding view. I can not make it work as I need.

Nested UserControl - it is nothing more than DatePickerand DropDownfor hours. How can I specify DatePickerto select the date passed to the ViewModel as the selected date?

I tried almost everything, and now I'm not far from the jump outside the window. As you can see, any help is appreciated;)

Now up to the code: DateTimePicker.xaml.cs (CodeBehind)

public partial class DateTimePicker
{
    public static DependencyProperty SelectedDateValueProperty = DependencyProperty.Register("SelectedDateValue", typeof (DateTime), typeof (DateTimePicker), new FrameworkPropertyMetadata(default(DateTime), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPropertyChangedCallback));

    private static void OnPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        Debug.WriteLine("Wohoo. I'm here and still debugging...");
    }

    public DateTimePicker()
    {
        InitializeComponent();

        DataContext = this;

        var times = GetTimes();

        Times.ItemsSource = times;
        Times.SelectedItem = times.First();
    }

    public DateTime SelectedDateValue
    {
        get { return (DateTime) GetValue(SelectedDateValueProperty); }
        set { SetValue(SelectedDateValueProperty, value); }
    }
}

Nested UserControl (DateTimePicker.xaml):

<UserControl x:Class="Framework.Controls.DateTimePicker"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         mc:Ignorable="d" 
         d:DesignHeight="30" d:DesignWidth="200"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*" />
        <ColumnDefinition Width="2*" />
    </Grid.ColumnDefinitions>
    <DatePicker HorizontalAlignment="Left" Name="DatePickerCalendar" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" SelectedDate="{Binding SelectedDateValue}" />
    <ComboBox Grid.Column="1" VerticalAlignment="Center" Name="Times" DisplayMemberPath="Name" />
</Grid>

And last but not least, a view that has a nested UserControl (View.xaml)

<CustomControls:DateTimePicker SelectedDateValue="{Binding LocalRegistrationStartDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

, , - , .

+3
2

:

"{Binding SelectedDateValue}"

WPF ", DataContext , SelectedDateValue".

, Property .

- , :

<UserControl x:Name="myControl"/>

:

"{Binding ElementName=myControl, Path=SelectedDateValue}"
+6

WPF , , , . Template, TemplateBinding, . . MSDN.

<ControlTemplate TargetType="{x:Type local:DateTimePicker>
  ...
  <DatePicker SelectedDate="{TemplateBinding SelectedDateValue}" />
  ...
</ControlTemplate>
+1

All Articles