问题描述
我有一个WPF窗口,它作为数据上下文绑定到viewmodel。点击按钮1,我隐藏了视图。
现在单击button2,视图再次可见,我必须将viewmodel的新对象作为datacontext给它
但scince窗口没有重新加载所以不不更新视图。
是否可以使用新的ViewModel更新视图以更改可见性?更新我的代码虽然它不是完整的mvvm但最终我将不得不在MVVM中实现它
我尝试过:
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:
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();
}
推荐答案
private MainWindowViewModel _CurrentViewModel;
public MainWindowViewModel CurrentViewModel
{
get { return _CurrentViewModel; }
set
{
_CurrentViewModel = value;
OnPropertyChanged(new PropertyChangedEventArgs("CurrentViewModel"));
}
}
有关 INotifyPropertyChanged 的实施,请参阅此CP文章, []
现在在Window Loaded Event中为 MainWindowViewModel 分配 DataContext ,例如,
Refer this CP article for INotifyPropertyChanged implementation, Implementing INotifyPropertyChanged[^]
Now assign DataContext for MainWindowViewModel in Window Loaded Event, like,
mn.DataContext = CurrentViewModel;
并在button2_Click和button3_Click中更改你的viewmodel,只需替换以下行,
and to change your viewmodel in button2_Click and button3_Click, just replace following line,
mn.DataContext = new MainWindowViewModel(lstVar, mnoList);
这一行,
with this line,
CurrentViewModel = new MainWindowViewModel(lstVar, mnoList);
if (this.DataContext != null)
this.DataContext = null;
this.DataContext = viewModel1;
此外,如果您的列表未更新,则代替列表为itemSource请通过指定的viewmodel尝试ObservableCollection
Also if your List is not updated then instead of giving List As itemSource please try ObservableCollection
through your assigned viewmodel
这篇关于如何将我的视图重新绑定到窗口可见性更改的新数据上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!