6

I'm building a simple UserControl example with DependencyProperties so that the properties of the control can be changed in XAML (code below).

But of course in my application I don't want this control to have tightly-coupled code-behind, but instead the user control will be a view called "DataTypeWholeNumberView" and it will have its own ViewModel called "DataTypeWholeNumberViewModel".

So I am going to implement the DependencyProperty logic below into the ViewModel, but in ViewModels I usually inherit INotifyPropertyChanged which seems to give me the same functionality.

So what is the relationship between:

  1. binding the DataContext of UserControl XAML to its code behind which has a DependencyProperties
  2. binding the DataContext of UserControl XAML (View) to its ViewModel (which inherits from INotifyPropertyChanged) and has properties which implements INotifyPropertyChanged functionality?

XAML:

<UserControl x:Class="TestDependencyProperty827.SmartForm.DataTypeWholeNumber"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal">
            <TextBlock Text="{Binding Label}"/>
        </StackPanel>
    </StackPanel>
</UserControl>

Code Behind:

using System.Windows;
using System.Windows.Controls;

namespace TestDependencyProperty827.SmartForm
{
    public partial class DataTypeWholeNumber : UserControl
    {
        public DataTypeWholeNumber()
        {
            InitializeComponent();
            DataContext = this;
        }

        public string Label
        {
            get
            {
                return (string)GetValue(LabelProperty);
            }
            set
            {
                SetValue(LabelProperty, value);
            }
        }

        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(string), typeof(DataTypeWholeNumber),
            new FrameworkPropertyMetadata());
    }
}
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • Related to question : http://stackoverflow.com/questions/291518/inotifypropertychanged-vs-dependencyproperty-in-viewmodel – Amitd Aug 21 '12 at 11:39

3 Answers3

9

INotifyPropertyChanged is an interface that exists in .Net since 2.0. It basically allows objects to notify when a property has changed. An interested party can perform certain actions when this event is raised. The problem with it is that it only publishes the name of the property. So you end up using reflection or some iffy if statements to figure out what to do in the handler.

DependencyProperties are a more elaborate construct that supports default values, change notifications in a more memory-efficient and performant way.

The only relationship is that the WPF binding model supports binding to either DependencyProperties or to standard Clr properties, with an INotifyPropertyChanged implementation. Your ViewModel could be a DependecyObject as well and the third option would be to bind to the ViewModel's DependencyProperties!

Kent Boogaart wrote a very interesting article on having a ViewModel be a POCO vs a DependencyObject.

Szymon Rozga
  • 17,971
  • 7
  • 53
  • 66
2

I don't really think there is a relationship between DependencyProperties and INotifyPropertyChanged. The only magic here is that the Binding classes/utils are smart enough to recognize a DependencyProperty and bind directly to it, or subscribe to the binding-target's notify-property-changed event and wait for that to fire.

Armentage
  • 12,065
  • 8
  • 33
  • 33
  • One snag with what you are doing is that your model is not a visual element. DependencyProperty requires that you inherit from FrameworkElement, and then you need to be in some visual tree, etc etc etc. It's kind of gross and gets pretty hairy if you look too close. – Armentage May 20 '09 at 13:25
  • Your comment is not entirely true. You can use Dependency Properties by inheriting from the DependencyObject base class. – Szymon Rozga May 20 '09 at 13:30
  • Are you sure a DependencyObject that is not in the VisualTree really works the same way as a FrameworkElement? I've had problems with this in the past. – Armentage May 20 '09 at 15:14
0

With WPF, you can either bind to DependencyProperties or to Properties which implement INotifyPropertyChanged. It's a matter of choice.

So your question breaks into either to have them in code behind or in view model. Since you have mentioned that you do not want a tightly-coupled code behind, you are better off having a view model following the MVVM pattern.

You can use the DependencyProperties even in your view model just like you have done in your code behind.

Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63