What is the difference between Property and Dependency Property

Dependency properties and standard properties are quite different. The key features delivered by dependency properties are support for binding and animation. If you want to assign a value to a property using a Binding or template binding that property needs to be a dependency property. When animating a property the a dependency property can track … Read more

How do You Create a Read-Only Dependency Property?

It’s easy, actually (via RegisterReadOnly): public class OwnerClass : DependencyObject // or DependencyObject inheritor { private static readonly DependencyPropertyKey ReadOnlyPropPropertyKey = DependencyProperty.RegisterReadOnly( nameof(ReadOnlyProp), typeof(int), typeof(OwnerClass), new FrameworkPropertyMetadata(default(int), FrameworkPropertyMetadataOptions.None)); public static readonly DependencyProperty ReadOnlyPropProperty = ReadOnlyPropPropertyKey.DependencyProperty; public int ReadOnlyProp { get { return (int)GetValue(ReadOnlyPropProperty); } protected set { SetValue(ReadOnlyPropPropertyKey, value); } } //your other code here … Read more

Twoway-bind view’s DependencyProperty to viewmodel’s property?

If you want to do it in XAML, you could try using styles to achieve that. Here’s an example: <UserControl x:Class=”MyModule.MyView” xmlns:local=”clr-namespace:MyModule”> <UserControl.Resources> <Style TargetType=”local:MyView”> <Setter Property=”MyViewProperty” Value=”{Binding MyViewModelProperty, Mode=TwoWay}”/> </Style> </UserControl.Resources> <!– content –> </UserControl> In your case both MyViewProperty and MyViewModelProperty would be named MyProperty but I used different names just to be … Read more

What’s the difference between a dependency property and an attached property in WPF?

Attached properties are a type of dependency property. The difference is in how they’re used. With an attached property, the property is defined on a class that isn’t the same class for which it’s being used. This is usually used for layout. Good examples are Panel.ZIndex or Grid.Row – you apply this to a control … Read more

What’s the difference between Dependency Property SetValue() & SetCurrentValue()

The MSDN link you provided says it quite well: This method is used by a component that programmatically sets the value of one of its own properties without disabling an application’s declared use of the property. The SetCurrentValue method changes the effective value of the property, but existing triggers, data bindings, and styles will continue … Read more

What is a dependency property?

The only explanation I found helpful and well written is this one: http://www.wpftutorial.net/dependencyproperties.html Basically, DependencyProperties differ from regular properties in that they’re not just setters / getters for fields in the class, but they retrieve their actual values dynamically during runtime. The SetValue() method of DPs is pretty straightforward and sets the local value of … Read more

How to correctly bind to a dependency property of a usercontrol in a MVVM framework

That is one of the many reasons you should never set the DataContext directly from the UserControl itself. When you do so, you can no longer use any other DataContext with it because the UserControl’s DataContext is hardcoded to an instance that only the UserControl has access to, which kind of defeats one of WPF’s … Read more