3

I have some philosophical question for you:

In my UWP apps, currently I usually define binding properties as follows:

public class ExampleViewModel : BaseBind {

    // Pattern used for two-way bindings
    private object exampleObject;  // Private field for backing a binding propery
    public Object ExampleObject {
        get { return exampleObject; }
        set {
            SetProperty(ref exampleObject, value);
            OnPropertyChanged(nameof(ExampleDerivateProperty));
        }
    }

    // Simple derivate property, used in One-way bindings
    public Object ExampleDerivateProperty => (<<Boolean function about ExampleObject>>)? "Something" : "Something different";

}

That's it... this is all that I currently use in my MVC pattern.

...but I noticed that many prefer using DependencyProperty for backing their binding properties:

public string MyProperty
{

    get { return (string)GetValue(MyProperty); }
    set { SetValue(MyProperty, value); }

}

public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(MyControl), null);

What new interesting functions could I obtain if I would use DependencyProperties instead of simple private fields?

I know that this question could seem already answered here on StackOverflow, but this particular case may be interesting to delve into, even with some example.

Thanks.

EDIT: I know there already are some answers to this topic, but they're mostly outdated, because now we have the UWP platform, which features "x:Bind" compile binding and many other things have been added to C#. I would like to know if something has changed since then.

Luca Lindholm
  • 813
  • 1
  • 9
  • 23
  • 1
    Possible duplicate of [When to use Dependency Properties](https://stackoverflow.com/questions/4104154/when-to-use-dependency-properties) – Wai Ha Lee Dec 12 '17 at 16:30
  • @WaiHaLee, yes, but that was back in 2010 and C# has evolved in the past few years: UWP platform was created, which features new "x:Bind" that I commonly use for XAML binding... so, I thought that this question may require an updated answer... – Luca Lindholm Dec 12 '17 at 16:33

1 Answers1

1

What new interesting functions could I obtain if I would use DependencyProperties instead of simple private fields?

Classes with dependency properties must inherit from DependencyObject and this has some drawbacks which are summarized here:

INotifyPropertyChanged vs. DependencyProperty in ViewModel

They can only be accessed from a single thread for example.

In general, a view model shouldn't contain any dependency properties. Only target properties are typically defined as dependency properties. Target properties are such properties that you bind something to in the view, for example the Text property of a TextBlock.

x:Bind has nothing to do with choosing between CLR and dependency source properties really. It's a markup extension in UWP that converts your XAML bindings into code at compile-time for performance reasons.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Good... if you could include some example of DependencyProperty and it's use (VS IDE states that they allows Animations, Templates, etc.), it would be great. – Luca Lindholm Dec 12 '17 at 16:51