Binding UserControl Properties to nested ViewModel

Topics: Questions
Sep 30, 2012 at 2:49 PM

Hi,

I have a CatelUserControl with ViewModel which I want to use like this:
<my:SearchBox InfoText="blah blah" />

To make this possible, I created a DependencyProperty InfoText on my.SearchBox's code behind.
I can show the content of this within the SearchBox:
<UserControl x:Name="userControl">
  <Label Content="{Binding Path="InfoText", ElementName="userControl" />
</UserControl>

The problem here is, I really want to have the InfoText value within the SearchBoxViewModel.
I do not need it on the control, only within the ViewModel of the control.
But I want to set the value by specifying a Property of the control on the parent xaml.
I have no clue how to get this... searched google for hours :-)

any idea?
regards, alex.

Coordinator
Sep 30, 2012 at 3:01 PM

Take a look at the ViewToViewModel attribute on the view. Then you can create a dependency property and forward it to your view model.

Sep 30, 2012 at 6:56 PM

Hi Geert,

as usual, catel surprises me with high flexibility and ease of use.
I should have known Catel provides a soluting.
In fact, Catel seems to provide a solution for nearly every thing,
at the latest after the next beta release... :-)

Thank you for this very quick reply.
It worked nearly out of the box, first I missed to put it into MappingType=ViewToViewModel mode.
Thought this would be the default because of the attribute name suggesting it.
But the outstanding logging capabilities easily showed what was wrong.

The [Commanding between Usercontrols] thread is solved using the same technique.
In fact I use messaging with individual Tag values provided by the master control to the child via ViewToViewModel binding.
Using <SearchControl ExecuteMessageTag="{Binding}" /> the controls ViewModel has a reference to the parent's ViewModel.
This reference is passed as Tag with the Message. The parent only accepts Messages with (this) as tag. Everyone is happy :-)

Best regards to the Netherlands,
Alex.

PS: On my research for a solution I found many replies telling it is impossible to get this working.... If they knew... :-)

Coordinator
Oct 1, 2012 at 6:40 AM

Thanks for your kind reply :-)

People all over the internet think they are "gurus", but apparently they don't even know what's possible. But that's the downside of free support, everybody is doing what they can to help each other. Just be glad you found catel ;-)