WPF .NET Core - Inside Dependency Properties
Written by Alex Armstrong   
Thursday, 28 May 2020
Article Index
WPF .NET Core - Inside Dependency Properties
Why Bother?
Binding

For example:

MyClass MyObject=new MyClass();
MyObject.MyProp = 3.141;
double pie = MyObject.MyProp;

stores the value 3.141 in MyDPProperty and retrieves the same value into pie. Notice that it is the dependency property that is storing the value the get and set CLR property simply provides access to it.

You can think of the CLR property as a "wrapper" for the dependency property if it helps.

This is the usual pattern for implementing a custom dependency property.

Also notice that now we have three "names" for the property

  • MyProp the CLR property,

  • MyDP the Dependency property name you registered

and

  • MyDPProperty the static variable referencing the DependencyProperty object.

In most cases the CLR property would be given the same name as the dependency property. That is MyDP in this instance.

So for example a Slider object uses the name"Value" - as a CLR property, as the name of a dependency property and uses ValueProperty as the static variable referencing the DependencyProperty object.

Banner

Binding

So far you have no proof that the MyProp CLR property really is backed by a dependency property - so let's demonstrate that it can be bound to a slider control.

It is assumed that you know the basics of data binding using code but don't worry if you don’t because it's easy and follows the XAML as long as you remember the XAML is just an object instantiator and initializer.

First we need to create a binding object initialised to the source object, i.e. MyObject in this case:

Binding myBinding = new Binding("MyDP");
myBinding.Source = MyObject;

The Binding object is "bound" to the MyDP dependency property but you also have to specify the instance of the object that has the dependency property that you want to use. 

Notice that the name of the property, i.e. MyDP is used rather than the static variable MyDPProperty. One of the problems with working with dependency objects is trying to guess which name or reference is required.

The binding object defines and controls the source of the data and all that remains is to connect it to the target using the SetBinding method, assuming the target provides one:

slider1.SetBinding(Slider.ValueProperty,
                                myBinding);

and assuming that there is a Slider control on the form called slider1.

Notice that in this case you have to provide not the name of the property but the static variable referencing it, i.e. ValueProperty rather than Value.

Also notice that the WPF framework doesn't use the get/set methods to access the property but goes straight to the dependency property. If you need to intercept a WPF update to a dependency property you can add a call back function to the metadata in the Register method.

If the object doesn't provide a SetBinding method you have to do the job using the BindingOperations static object:

BindingOperations.SetBinding(
                         slider1,
                         Slider.ValueProperty,
                         myBinding);

The result is the same.

Now MyDP dependency property is bound to slider1's ValueProperty dependency property. If you now do:

MyObject.MyProp = 3.141;

you will see the slider move on the form to a position that corresponds to 3.141.

You can also do the job the other way round with MyObject as the source and slider1 as the target:

Binding myBinding = new Binding("Value");
myBinding.Source = slider1;
BindingOperations.SetBinding(
                       MyObject,
                       MyClass.MyDPProperty,
                       myBinding);

but in this case you have to use BindingOperations because the property doesn't implement a SetBinding method - but it could. Now when you change the slider's position the value of MyDP is changed accordingly.

Of course you could set a two-way data binding using the Mode property and then which is the source and which is the target doesn’t make a lot of difference.

Can you use the MyClass and its MyDP dependency property in XAML?

Yes of course - but that is another story.

XAML

 

The Programmer's Guide To
WPF .NET Core

wpfcover

Contents

  1. Getting Started with WPF
  2. Getting To Know WPF 
  3. Creating Objects With XAML
  4. Inside Dependency Properties 
  5. Routed Events ***NEW
  6. Simple WPF data binding
  7. FlexGrid - A Lightweight Data Grid
  8. Using the WPF .NET 4.0 DataGrid
  9. ISupportInitialize and XAML
  10. WPF The Easy 3D Way
  11. BitmapSource: WPF Bitmaps
  12. Loading Bitmaps: DoEvents and the closure pattern
  13. Bitmap Effects
  14. Custom Bitmap Effects - Getting started
  15. Custom Bitmap Effects - HLSL
  16. Custom BitmapSource
  17. Bitmap Coding and Metatdata in WPF
  18. Custom Shape
  19. The bitmap transform classes
  20. Drawing Bitmaps – DrawingImage and DrawingVisual
  21. RenderTargetBitmap - Visual vector to bitmap
  22. WriteableBitmap
  23. BitmapImage and local files

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

Banner


WPF .NET Core - Routed Events

Routed events are a key feature of WPF. We look at bubbling and tunnelling and how to create your own routed event and how WPF changes the underlying mechanics of Windows.



Bitmap Coding and Metatdata in WPF


Having looked at working with raw pixel data we turn our attention to formattted image files and how to code and decode both the pixel data and the meta data they contain.

 



Custom Bitmap Effects - HLSL

In the article Custom Bitmap Effects - Getting started we discovered how to work with HLSL in WPF. Now we are in a position to write more sophisticated shaders and  this means learning some more  [ ... ]



RenderTargetBitmap - Visual vector to bitmap

RenderTargetBitmap will convert any Visual to a bitmap but sometimes it isn't quite as straighforward as just calling Render().



Loading Bitmaps: DoEvents and the closure pattern

Sometimes loading a bitmap causes an asynchronous download and you have to wait for it to complete before using it - but how best to wait? The standard solution is to use an event but this breaks what [ ... ]


Other Articles

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:0470041803>

<ASIN:1590599624>

<ASIN:0321374479>

 



Last Updated ( Thursday, 28 May 2020 )