Binding from View-Model to View-Model of a child User Control in Silverlight? 2 sources - 1 target..

Posted by andrej351 on Stack Overflow See other posts from Stack Overflow or by andrej351
Published on 2010-03-18T03:41:01Z Indexed on 2010/03/18 4:11 UTC
Read the original article Hit count: 632

Hi there,

So i have a UserControl for one of my Views and have another 'child' UserControl inside that.

The outer 'parent' UserControl has a Collection on its View-Model and a Grid control on it to display a list of Items.

I want to place another UserControl inside this UserControl to display a form representing the details of one Item.

The outer / parent UserControl's View-Model already has a property on it to hold the currently selected Item and i would like to bind this to a DependancyProperty on the inner / child UserControl. I would then like to bind that DependancyProperty to a property on the child UserControl's View-Model.

I can then set the DependancyProperty once in XAML with a binding expression and have the child UserControl do all its work in its View-Model like it should.

The code i have looks like this..

Parent UserControl:

<UserControl x:Class="ItemsListView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{Binding Source={StaticResource ServiceLocator}, Path=ItemsListViewModel}">
    <!-- Grid Control here... -->
    <ItemDetailsView Item="{Binding Source={StaticResource ServiceLocator}, Path=ItemsListViewModel.SelectedItem}" />
</UserControl>

Child UserControl:

<UserControl x:Class="ItemDetailsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{Binding Source={StaticResource ServiceLocator}, Path=ItemDetailsViewModel}"
    ItemDetailsView.Item="{Binding Source={StaticResource ServiceLocator}, Path=ItemDetailsViewModel.Item, Mode=TwoWay}"> 
    <!-- Form controls here... -->
</UserControl>

The selected Item is bound to the DependancyProperty fine. However from the DependancyProperty to the child View-Model does not.

It appears to be a situation where there are two concurrent bindings which need to work but with the same target for two sources.

Why won't the second (in the child UserControl) binding work?? Is there a way to acheive the behaviour I'm after??

Cheers.

© Stack Overflow or respective owner

Related posts about mvvm

Related posts about databinding