locked
Binding Multiple layers of Nested Controls RRS feed

  • Question

  •  I have a user control called EditControl with a datagrid.  Within the datagrid I have an edit button that is bound to my view model.  I used a proxy class to get the nested binding to work between the button in the datagrid and my view model. The code is shown below:

     public class DataContextProxy: FrameworkElement
        {
            public DataContextProxy()
            {
                this.Loaded += new RoutedEventHandler(DataContextProxy_Loaded);
            }
    
            void DataContextProxy_Loaded(object sender, RoutedEventArgs e)
            {
                Binding binding = new Binding();
                if (!String.IsNullOrEmpty(BindingPropertyName))
                {
                    binding.Path = new PropertyPath(BindingPropertyName);
                }
                binding.Source = this.DataContext;
                binding.Mode = BindingMode;
                this.SetBinding(DataContextProxy.DataSourceProperty, binding);             
            }
    
            public Object DataSource
            {
                get { return (Object)GetValue(DataSourceProperty); }
                set { SetValue(DataSourceProperty, value); }
            }
    
            public static readonly DependencyProperty DataSourceProperty =
                DependencyProperty.Register("DataSource", typeof(Object), typeof(DataContextProxy), null);
    
    
            public string BindingPropertyName { get; set; }
    
            public BindingMode BindingMode { get; set; }
    
        }


     

     

        <UserControl.Resources>
            <controls:DataContextProxy x:Key="DataContextProxy"/>
        </UserControl.Resources>
    
    
    
    <sdk:DataGridTemplateColumn x:Name="editButtonColumn" Width="100">
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                    <HyperlinkButton Content="Edit" Width="50" Height="20" Command="{Binding Source={StaticResource DataContextProxy},Path=DataSource.EditCommand}" CommandParameter="{Binding Path=Email.ID}" />
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>


     

     

    This works great when I show the control within a ContentControl of Control1.  But what I need to do is show EditControl within a ContentControl of Control2, when Control2 is shown within a ContentControl of Control1.  The bindings do not appear to work in this situation.

    I've attached my code.

    How can I get this to work?

     

     

     

    Monday, July 26, 2010 2:50 PM

Answers

  • I'm setting my EditViewModel to a new model when loading Control2 and all is working now. 

    Monday, July 26, 2010 4:16 PM

All replies

  • Additional information:

    When the EditControl is loaded in Control1, the "this.DataContext" in DataContextProxy_Loaded is the appropirate view model.

    When loaded in Control2, that is loaded in Control1, the "this.DataContext" is null.

     

    Monday, July 26, 2010 4:05 PM
  • I'm setting my EditViewModel to a new model when loading Control2 and all is working now. 

    Monday, July 26, 2010 4:16 PM