如何将自定义依赖属性绑定到控件的视图模型?

时间:2015-06-05 08:46:51

标签: c# wpf dependency-properties

我需要创建一个具有少量输入/输出且具有大量内部功能的控件。我认为最好的方法是创建Dependency Properties以与其他应用程序部分进行交互,并使private view model具有隐藏功能。

这是我的样本:

窗口

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:app="clr-namespace:WpfApplication1">
<StackPanel>
    <DatePicker x:Name="DatePicker" />
    <app:MyControl DateCtrl="{Binding ElementName=DatePicker, Path=SelectedDate}" />
</StackPanel>
</Window>

MyControl

<UserControl x:Class="WpfApplication1.MyControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:app="clr-namespace:WpfApplication1">
<UserControl.DataContext>
    <app:ViewModel />
</UserControl.DataContext>
<Grid>
        <TextBlock Text="{Binding DateVM}" />
</Grid>
</UserControl>

控制代码隐藏

using System;
using System.Windows;

namespace WpfApplication1
{
public partial class MyControl
{
    public MyControl()
    {
        InitializeComponent();
    }

    public static DependencyProperty DateCtrlProperty = DependencyProperty.Register("DateCtrl", typeof(DateTime), typeof(MyControl));
    public DateTime DateCtrl
    {
        get { return (DateTime) GetValue(DateCtrlProperty); }
        set { SetValue(DateCtrlProperty, value); }
    }
}
}

视图模型

using System;
using System.ComponentModel;

namespace WpfApplication1
{
    public class ViewModel : INotifyPropertyChanged
    {
        private DateTime _dateVM;
        public DateTime DateVM
        {
            get { return _dateVM; }
            set
            {
                _dateVM = value;
                OnPropertyChanged("DateVM");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

我需要实现的是将date中选择的DatePicker传播到MyControl's view model。或者,有没有更好的模式可供使用?

2 个答案:

答案 0 :(得分:3)

您所描述的是一种常见的误解,即所有视图都应具有视图模型。但是,对于UserControl来说,使用自己的DependencyProperty来作为控件通常会更简单(也更合适)。

您的方法存在的问题是您已在内部分配了UserControl DataContext,因此无法从控件外部进行设置。解决方案是在内部设置DataContext,而是使用RelativeSource Binding来访问UserControl DependencyProperty,如下所示:

<TextBlock Text="{Binding DateCtrl, RelativeSource={RelativeSource 
    AncestorType={x:Type YourLocalPrefix:MyControl}}}" />

如果你真的必须使用内部视图模型,那么声明该类型的DependencyProperty和数据绑定到它,就像我上面显示的那样:

<TextBlock Text="{Binding YourViewModelProperty.DateVM, RelativeSource={RelativeSource 
    AncestorType={x:Type YourLocalPrefix:MyControl}}}" />

答案 1 :(得分:0)

DatePicker应该拥有自己的DatePickerViewModel,它定义了SelectedDate属性或依赖属性。您应该定义DatePicker控件的XAML以使用此专用ViewModel。

然后,当您使用控件时,可以像这样设置绑定:

<DatePicker SelectedDate="{Binding Path=DateVM, 
            Mode=TwoWay, 
            UpdateSourceTrigger=PropertyChanged}" />

请注意,DateVM不是日期选择器的属性,而是消费视图模型(或在本例中为主窗口)上的属性。当选择器打开时,它将默认为DateVM中已设置的日期。

另一件事是你的选择器目前不允许任何选择 - 它只是一个文本块!