ViewModel usage with Attached Property

May 24, 2012 at 12:31 PM

Hi Geert,

we are using an attached property with a usercontrol to manage the viewstate of the control. The value of the property is set via binding from a viewmodel property.

The usercontrol contains several  ui elements with bindings to the same viewmodel. When the usercontrol is instantiated, all bindings are refreshed from the current viewmodel of the usercontrol but the attached property has a bindung error, because the viewmodel at time of binding is the container viewmodel of the surrounding page.

I am using Silverlight with Catel 3.1.

Could you please help?!
Best Regards

Rolf

 

Coordinator
May 24, 2012 at 12:36 PM

Interesting, what kind of attached property is it? Strange that it isn't getting the binding update correctly.

May 25, 2012 at 8:20 AM

The properties in the remaining xaml of the usercontrol has no binding errors but when binding VisualStateName the parent viewmodel is active.

<catel:UserControl

.

.

.

Helpers:StateManager.VisualStateProperty="{Binding VisualStateName}"

>

.

.

Coordinator
May 25, 2012 at 8:25 AM

And VisualStateName is located on the VM on the parent? Or on the VM of the UC?

May 25, 2012 at 9:10 AM

VisualStateName is located on the VM of the UC.


Von: GeertvanHorrik [notifications@codeplex.com]
Gesendet: Freitag, 25. Mai 2012 09:25
An: rh@devteam.eu
Betreff: Re: ViewModel usage with Attached Property [Catel:356975]

From: GeertvanHorrik

And VisualStateName is located on the VM on the parent? Or on the VM of the UC?

Coordinator
May 26, 2012 at 5:37 PM

Interesting, do you have a small repro so I can fix this issue?

Thanks in advance.

May 29, 2012 at 8:41 AM

Thank you for your support. I hope I can help with this test programm.


Von: GeertvanHorrik [notifications@codeplex.com]
Gesendet: Samstag, 26. Mai 2012 18:37
An: rh@devteam.eu
Betreff: Re: ViewModel usage with Attached Property [Catel:356975]

From: GeertvanHorrik

Interesting, do you have a small repro so I can fix this issue?

Thanks in advance.

Coordinator
May 29, 2012 at 7:49 PM

The reason that this doesn’t work is that the usercontrol creates an inner data context. Basically, it does something like this:


UC DataContext

  |= Inner grid with VM as DataContext

        |= Your control content

  

The inner grid is injected between the user control data context and the content, so you must declare the attached property bindings lower than the content root:


<UserControl etc>

    <catel:StackGrid MyClass.MyAttachedProperty=”{Binding MyProperty}”>

    <!--your content here -->

   </catel:StackGrid>>

</UserControl>