MVVM Databinding: Challenges

Coordinator
Aug 25, 2009 at 3:34 AM
Edited Aug 25, 2009 at 3:35 AM

I've run into two cases where I couldn't bind directly into Xaml from my view models:

  1. Attached Properties.  Things like Canvas.Left="{Binding Left}" don't work, and for obvious reasons I would guess.  But it would be nice for the binding pipeline to understand this kind of binding and handle it appropriately.
  2. Perspective 3D rotation transforms.  This appears to work when performing Element-To-Element binding, but when I do this RotationX="{Binding RotationXAmount}" I get a Xaml error at runtime...

The workaround:

I setup a registration process to the view model so that UserControl views needing to participate in binding for properties mentioned above, simply register to their view model like so:

VM.RegisterPositionBoundElement(this); //for Top/Left binding

VM.RegisterProjectBoundElement(this); // for perspective binding

Then in the view model, I iterate through any registered elements whenever the appropriate property is changed by another control bound to that property, like this example for the "Left" property:

private double _left;
public double Left
{
    get { return _left; }
    set
    {
        _left = value;
        foreach (var e in PositionBoundElementList)
        {
            Canvas.SetLeft(e, _left);
        }
        PropertyChanging("Left");
    }
}

So for this discussion, the question is:   Is there a better approach to this problem in an MVVM patterned application?