WPF-MVVM将ViewModel-Property绑定到嵌套的UserControl

时间:2012-04-18 15:03:39

标签: wpf mvvm binding user-controls dependency-properties

正如标题所说,我想将一个属性从我的ViewModel绑定到相应视图中的嵌套UserControl。 我不能按照我需要的方式工作。

嵌套的UserControl只是一小时的DatePickerDropDown。如何告诉DatePicker选择ViewModel传播的日期作为其选定日期?

我几乎尝试了一切,现在我离窗户不远了。 正如您所见,任何帮助表示赞赏;)

到目前为止的代码: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); }
    }
}

嵌套的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>

最后但并非最不重要:具有嵌套UserControl的View(View.xaml)

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

希望问题很清楚,任何人都可以帮助我,或者说明我在这里做错了什么。

2 个答案:

答案 0 :(得分:6)

使用:

"{Binding SelectedDateValue}"

告诉WPF“嘿,请检查我的DataContext以查找名为SelectedDateValue的媒体资源。”

您想要的是从用户控件获取Property

最简单的方法是为用户控件命名,如:

<UserControl x:Name="myControl"/>

然后将绑定修改为:

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

答案 1 :(得分:1)

实现WPF控件的常用方法是使用模板而不是将控件定义为直接内容,就像您在此处所做的那样。通过使用Template,您可以访问TemplateBinding,从而可以轻松绑定控件属性。请参阅Control Customization MSDN页面。

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