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

3
正如标题所述,我想将来自我的ViewModel的属性绑定到相应视图中嵌套的UserControl。 我无法以需要的方式使其工作。
嵌套的UserControl只是一个DatePicker和一个下拉框,用于小时数。 我如何告诉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); }
    }
}

嵌套的用户控件(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个回答

6

使用:

"{Binding SelectedDateValue}"

告诉WPF“嘿,检查我的中是否有名为的属性”。

你想要的是从用户控件中获取属性。

最简单的方法是给你的用户控件起一个名称,例如:

<UserControl x:Name="myControl"/>

然后修改您的绑定:

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

谢谢您的回复。我给我的UserControl命名,并按照您的描述更改了此UserControl中的绑定。在运行时,它告诉我,“错误:'DateTimePicker'上未找到LocalRegistrationStartDate属性的'object'...”,这是我的UserControl。那么我是设置了错误的绑定吗? - KingKerosin
好的。现在似乎可以工作了。我不得不删除其他论坛中找到的一些东西,因为它们没有做我想要的事情;-) - KingKerosin

1
WPF控件通常的实现方式是使用模板而不是像您在这里所做的那样定义直接内容。通过使用Template,您可以访问TemplateBinding,从而轻松地绑定控件属性。请参阅Control Customization MSDN页面。
<ControlTemplate TargetType="{x:Type local:DateTimePicker>
  ...
  <DatePicker SelectedDate="{TemplateBinding SelectedDateValue}" />
  ...
</ControlTemplate>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接