Message.Attach binding from an attached property?

Feb 18, 2011 at 10:30 AM

Is there a Caliburn mechanism which allows a view model property to be bound to an arbitrary attached property defined on an element?

Feb 18, 2011 at 1:35 PM

Can you give some more details, perhaps a specific scenario you are working on?

Feb 18, 2011 at 1:57 PM

Sure, it goes to the problem with the PasswordBox.   I want to be able to show the user why their password is causing a validation failure (or not).  I'd like to do this consistently and use an error template (the same one being used for other controls on the form). A problem with doing this arises because the underlying Password property is not a dependency property so edits don't cause binding actions.  gmnicol's post encouraged me to revisit using an attached property solution. 

In the example he referenced it shows the password being reflected in a TextBlock by binding (in Xaml) to a TextBlock an attached property which has been specifically crafted to mimic binding the PasswordBox.Password property.  Here's the example from the post and which I've replicated my code.  It works when the binding is in Xaml between the attached property and another element:

<PasswordBox w:PasswordHelper.Attach="True" Width="130"
         w:PasswordHelper.Password="{Binding Text, ElementName=plain, Mode=TwoWay}"
<TextBlock Padding="10,0" x:Name="plain" />

However binding the attached property to a view model property (using Xaml) in the hope of being able to have IDataErrorInfo show errors doesn't seem to work. That is, the view model property is *never* called.  I've tried creating the binding between the attached property and the view model property in code but again the view model property is not called.  So my next trial is to see if the binding can be created using Message.Attach which is the reason for the question.  Anyway, I'll learn something - even if it's that I'm barking up the wrong tree. 

Thanks for your help and let me know if an example focused on just this problem will help.


Feb 18, 2011 at 7:04 PM

It should be just:

<PasswordBox w:PasswordHelper.Attach="True" Width="130"
         w:PasswordHelper.Password="{Binding SomePropertyOnTheViewModel, Mode=TwoWay}" 

being SomePropertyOnTheViewModel, well..., a property on the ViewModel actually set as DataContext for the view.
If it doesn't work, there should be some other issue around...

Feb 18, 2011 at 11:46 PM

Thanks for your comment. Yes, I thought that might be it but in my example, when working with the PasswordBox, it doesn't work.  That is, the SomePropertyOnTheViewModel (your example) does not get called.  Anyway, I got a solution worked out using Message.Attach is the end (none of which to do with Caliburn and all to do with a strange design decision by the authors of PasswordBox.

Feb 19, 2011 at 8:02 PM

The custom attached property is aimed to overcome exaclty the PasswordBox binding oddity.
Looking at the code, PasswordHelper.Password should exhibit a plain two-way binding behavior, regardless of PasswordBox design.
Glad you found another perfectly viable solution; yet, I think the unsuccesful attempt with the attached property was due to some other piece out of square.