Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WPF Window which is bind to viewmodel as data context. On click of a button1 I hide the view.
Now on click of button2 the view is again visible and I have to give a new object of viewmodel as datacontext to it
but scince the window is not reloaded so that doesn't updates the view.

Is it possible to update the view with new ViewModel on visiblity change? Updating my code though its not full mvvm but ultimately i will have to implement it in MVVM

What I have tried:

C#
MainWindow mn; // this is my view

 private void button2_Click(object sender, RoutedEventArgs e)
        {
            IList<Variable> lstVar = new List<Variable>();
            IList<string> mnoList = new List<string>();

            mnoList.Add("MNO1");
            mnoList.Add("MNO2");
            mnoList.Add("MNO3");
            mnoList.Add("MNO4");
            mnoList.Add("MNO5");

            IList<string> mnoList1 = new List<string>();
            mnoList1.Add("MNO4");
            mnoList1.Add("MNO5");

            Variable vr = new Variable("Var123", "opc:123", mnoList1);
            lstVar.Add(vr);
             mn = new MainWindow();
            mn.DataContext = new MainWindowViewModel(lstVar, mnoList);// viewmodel given as datacontext
            mn.ShowDialog();
           
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            mn.Visibility = Visibility.Hidden;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            IList<Variable> lstVar = new List<Variable>();
            IList<string> mnoList = new List<string>();

            mnoList.Add("MNO1");
           

            IList<string> mnoList1 = new List<string>();
            mnoList1.Add("MNO4");
            mnoList1.Add("MNO5");

            Variable vr = new Variable("Var123", "opc:123", mnoList1);
            lstVar.Add(vr);
            //mn = new MainWindow();
            mn.DataContext = new MainWindowViewModel(lstVar, mnoList);// giving my view datacontext a new object of view model
            mn.ShowDialog();
        }
Posted
Updated 22-Mar-16 22:49pm
Comments
VR Karthikeyan 22-Mar-16 14:04pm    
Hi, Create property of type MainWindowViewModel, Implement INotifyPropertyChanged for this proprty. And now assign this property as DataContext for your view. Refer the solution 1.

Hi, Create property of type MainWindowViewModel, and implement INotifyPropertyChanged interface like this,
C#
private MainWindowViewModel _CurrentViewModel;

public MainWindowViewModel CurrentViewModel
{
   get { return _CurrentViewModel; }
   set 
   { 
      _CurrentViewModel = value;
      OnPropertyChanged(new PropertyChangedEventArgs("CurrentViewModel"));
   }
}

Refer this CP article for INotifyPropertyChanged implementation, Implementing INotifyPropertyChanged[^]
Now assign DataContext for MainWindowViewModel in Window Loaded Event, like,
C#
mn.DataContext = CurrentViewModel;

and to change your viewmodel in button2_Click and button3_Click, just replace following line,
C#
mn.DataContext = new MainWindowViewModel(lstVar, mnoList);

with this line,
C#
CurrentViewModel = new MainWindowViewModel(lstVar, mnoList);
 
Share this answer
 
v2
You Just Need To Write this Code for Changing your DataContext on button click Event

if (this.DataContext != null)
                this.DataContext = null;

            this.DataContext = viewModel1;


Also if your List is not updated then instead of giving List As itemSource please try ObservableCollection

through your assigned viewmodel
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900