Click here to Skip to main content
15,904,156 members
Articles / Programming Languages / C#

Use dataBinding and DependencyProperty with a non-WPF or extern object

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
19 Feb 2010CPOL 7.5K   3  
Today we are going to learn how we can uses the powerful data binding of WPF even on non-WPF objects.

Introduction

Today we are going to learn how we can use the powerful data binding of WPF even on non-WPF objects.

The Problem: When to Use this Solution?

Sometimes you need to use the databinding with an object that you have not created and you can't use inheritance.

For example, I wanted to use a Mogre Camera and build some WPF animation with it and I couldn't because:

  • WPF animation needed a DependencyProperty to manipulate,
  • I couldn't derive the Mogre.Camera class and add it the correct things because I got the camera from a factory object (the sceneManager).

Then I couldn't use the WPF animation to move my camera... next is how I solve it.

A Solution: Mine and Surely Not the Best :)

Here is the solution I use:

  1. Create a proxy DependencyProperty inside a DependencyObject, for example your main windows.
  2. Override the OnPropertyChanged handler.
  3. Update the aimed attribute inside the handler.

To control my camera, I created this DependencyProperty inside my windows:

C#
public static readonly DependencyProperty CameraPositionProperty = 
	DependencyProperty.Register("CameraPosition", typeof(Point3D), 
	typeof(SurfaceWindow1), new PropertyMetadata(new Point3D(0, 0, 0)));
 
      public Point3D CameraPosition
      {
         get
         {
            return (Point3D)GetValue(CameraPositionProperty);
         }
         set
         {
            SetValue(CameraPositionProperty, value);
         }
      }

Then I added this override:

C#
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
      {
         base.OnPropertyChanged(e);
         if (e.Property.Name.Equals("CameraPosition"))
         {
            _ogre.Camera.SetPosition((float)((Point3D)e.NewValue).X, 
		(float)((Point3D)e.NewValue).Y, (float)((Point3D)e.NewValue).Z);
            _ogre.Camera.LookAt(Vector3.ZERO);
         }
      }

And my usual question:

Does someone have a better idea to perform the same?

This article was originally posted at http://blog.lexique-du-net.com/index.php

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer http://wpf-france.fr
France (Metropolitan) France (Metropolitan)
Jonathan creates software, mostly with C#,WPF and XAML.

He really likes to works on every Natural User Interfaces(NUI : multitouch, touchless, etc...) issues.



He is awarded Microsoft MVP in the "Client Application Development" section since 2011.


You can check out his WPF/C#/NUI/3D blog http://www.jonathanantoine.com.

He is also the creator of the WPF French community web site : http://wpf-france.fr.

Here is some videos of the projects he has already work on :

Comments and Discussions

 
-- There are no messages in this forum --