Silverlight data binding to parent user control's properties with using MVVM in both controls

Posted by MagicMax on Stack Overflow See other posts from Stack Overflow or by MagicMax
Published on 2010-05-17T10:32:16Z Indexed on 2010/05/17 10:41 UTC
Read the original article Hit count: 1288

Filed under:
|
|
|

Hello!

I have two UserControls ("UserControlParentView" and "UserControlChildView") with MVVM pattern implemented in both controls. Parent control is a container for Child control and child control's property should be updated by data binding from Parent control in order to show/hide some check box inside Child control.

Parent Control Description

UserControlParentViewModel has property:

    private bool isShowCheckbox = false;
    public bool IsShowCheckbox
    {
        get { return isShowCheckbox; }
        set { isShowCheckbox = value; NotifyPropertyChanged("IsShowCheckbox");  }
    }

UserControlParentViewModel - how I set DataContext of Parent control:

    public UserControlParentView()
    {
        InitializeComponent();

        this.DataContext = new UserControlParentViewModel();
    }        

UserControlParentView contains toggle button (in XAML), bound to UserControlParentViewModel's property IsShowCheckbox

<ToggleButton Grid.Column="1" IsChecked="{Binding IsShowCheckbox, Mode=TwoWay}"></ToggleButton>

Also Parent control contains instance of child element (somewhere in XAML)

<local:UserControlChildView IsCheckBoxVisible="{Binding IsShowCheckbox}" ></local:UserControlChildView>

so property in child control should be updated when user togggle/untoggle button. Child control contains Boolean property to be updated from parent control, but nothing happened! Breakpoint never fired!

Property in UserControlChildView that should be updated from Parent control (here I plan to make chechBox visible/hidden in code behind):

    public bool IsCheckBoxVisible
    {
        get { return (bool)GetValue(IsCheckBoxVisibleProperty); }
        set { SetValue(IsCheckBoxVisibleProperty, value); }
    }
    // Using a DependencyProperty as the backing store for IsCheckBoxVisible.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsCheckBoxVisibleProperty =
        DependencyProperty.Register("IsCheckBoxVisible", typeof(bool), typeof(TopMenuButton), new PropertyMetadata(false));

So the question is - what I'm doing wrong? Why child's property is never updated? BTW - there is no any binding error warnings in Output window...

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about binding