【问题标题】:WPF-MVVM Binding ViewModel-Property to nested UserControlWPF-MVVM 将 ViewModel-Property 绑定到嵌套的 UserControl
【发布时间】:2012-04-18 15:03:38
【问题描述】:

正如标题所说,我想将 ViewModel 中的属性绑定到相应视图中的嵌套 UserControl。 我无法让它按我需要的方式工作。

嵌套的 UserControl 只不过是几个小时的 DatePickerDropDown。如何告诉DatePicker 选择 V​​iewModel 传播的日期作为其选定日期?

我几乎尝试了所有方法,现在我离跳到窗外不远了。 如您所见,感谢您提供任何帮助;)

现在到目前为止的代码: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.xaml) 的视图

&lt;CustomControls:DateTimePicker SelectedDateValue="{Binding LocalRegistrationStartDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt;

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

【问题讨论】:

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


    【解决方案1】:

    使用:

    "{Binding SelectedDateValue}"
    

    告诉 WPF “嘿,检查我的 DataContext 是否有一个名为 SelectedDateValue 的属性”。

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

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

    <UserControl x:Name="myControl"/>
    

    然后将您的绑定修改为:

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

    【讨论】:

    • 感谢您的回复。我给了我的 UserControl 一个名称,并按照您的描述更改了此 UserControl 中的绑定。在运行时它告诉我,error: LocalRegistrationStartDate property not found on 'object' ''DateTimePicker'' ... 这是我的用户控件。那我是不是设置了错误的绑定?
    • 好的。似乎现在可以工作了。我不得不删除在其他论坛中发现的其他一些我不希望他们做的东西;-)
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多