Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding from View-Model to View-Model of a child User Control in Silverlight? 2 sources - 1 target
    text
    copied!<p>So i have a UserControl for one of my Views and have another 'child' UserControl inside that. </p> <p>The outer 'parent' UserControl has a Collection on its View-Model and a Grid control on it to display a list of <code>Items</code>. </p> <p>I want to place another UserControl inside this UserControl to display a form representing the details of one <code>Item</code>. </p> <p>The parent UserControl's View-Model already has a property on it to hold the currently selected <code>Item</code> and i would like to bind this to a DependancyProperty on the child UserControl. I would then like to bind that DependancyProperty to a property on the child UserControl's View-Model. </p> <p>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.</p> <p>The code i have looks like this..</p> <p>Parent UserControl:</p> <pre><code>&lt;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}"&gt; &lt;!-- Grid Control here... --&gt; &lt;ItemDetailsView Item="{Binding Source={StaticResource ServiceLocator}, Path=ItemsListViewModel.SelectedItem}" /&gt; &lt;/UserControl&gt; </code></pre> <p>Child UserControl:</p> <pre><code>&lt;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}"&gt; &lt;!-- Form controls here... --&gt; &lt;/UserControl&gt; </code></pre> <p><strong>EDIT:</strong> Heres how i created the Dependancy Proeprty on the child UC:</p> <pre><code>public partial class ItemDetailsView : UserControl { private static readonly DependencyProperty itemProperty; static ItemDetailsView() { ItemDetailsView.itemProperty = DependencyProperty .Register("Item", typeof(Item), typeof(ItemDetailsView), null); } public Item Item { get { return (Item)GetValue(ItemDetailsView.itemProperty); } set { SetValue(ItemDetailsView.itemProperty, value); } } public static Item GetItem(DependencyObject target) { return (Item)target.GetValue(itemProperty); } public static void SetItem(DependencyObject target, Item value) { target.SetValue(itemProperty, value); } } </code></pre> <p>The selected <code>Item</code> is bound to the DependancyProperty fine. However from the DependancyProperty to the child View-Model does not. </p> <p>It appears to be a situation where there are two concurrent bindings which need to work but with the same target for two sources.</p> <p>Why won't the second (in the child UserControl) binding work?? Is there a way to acheive the behaviour I'm after??</p> <p>Cheers.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload