WpfBegnner
WpfBegnner

Reputation: 319

WPF,MVVM Binding ViewModel-Property to nested UserControl

I have 2 user controls called "MyTree" and "MyGrid". I have another user control called "Content" which has a "MyTree" and "MyGrid" in it. I have a window with a tab control. Each tab item contains "Content". Tab items are added dynamically. So how can I add item to "MyGrid" when I create a tab item dynamically. I use MVVM patterns and INotifyPropertyChanged events Iam setting the item source as some property.

Now My "ContainerPanelViewModel" has

private string pro11 ;
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                MessageBox.Show("Enterd loop");
                PropertyChanged(this, e);
            }
        }
        public string pro1
        {
            get
            { 
                return pro11; 
            }
            set 
            {
                if (pro11 != value)
                {
                    pro11 = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("pro1"));
                }

            }
        }

And i have another view model which has a

ObservableCollection<ContainerPanelViewModel> RootNodeTabCollection

And Iam adding

 RootNodeTabCollection[0].pro1 = "abc";

But the label content is not getting updated

if (PropertyChanged != null)

is false always..and not entering to the loop. The message box is not displayed any time

Upvotes: 0

Views: 670

Answers (2)

Martin Liversage
Martin Liversage

Reputation: 106906

If PropertyChanged is null it means that nothing is bound to the view model. In your XAML you need to bind a property to an instance of your view model using the {Binding ...} syntax for anything to happen when you update a property on the view model.

You probably already have some bindings in place, but you can debug these bindings to learn more about why a binding is failing. There are several ways to do that but one method is to add PresentationTraceSources.TraceLevel=High to the binding. E.g.:

ItemsSource="{Binding Items, PresentationTraceSources.TraceLevel=High}"

WPF will then write binding trace information for that particular binding to the debug window.

Upvotes: 1

dmitrygrig
dmitrygrig

Reputation: 61

Likely you have a collection of ViewModels where each of them will be a DataContext for a certain Tab. Then this ViewModel should have another collection that can be used as DataContext for your Grid or Tree.

Upvotes: 1

Related Questions