1、<Frame x:Name="frameChild" Source="1UserControl.xaml" Navigated="frameChild_Navigated" NavigationUIVisibility="Hidden" Margin="5"/>
2、public 类名VM
{
get
{
return this.DataContext as 类名;
}
}
private void frameChild_Navigated(object sender, NavigationEventArgs e)
{
if ((string)cmbx_mac.SelectedItem == "某个项")
{
this.VM.1Vm = (this.frameChild.Content as UserControl).DataContext as 第一个ViewModel;
}
else if((string)cmbx_mac.SelectedItem=="另一项")
{
this.VM.2Vm = (this.frameChild.Content as UserControl).DataContext as 第二个ViewModel;
}
}
3、
this.frameChild.Loaded += frameChild_Loaded;
void frameChild_Loaded(object sender, RoutedEventArgs e)
{
if ((string)cmbx_mac.SelectedItem == "某项")
{
this.VM.1Vm = (this.frameChild.Content as UserControl).DataContext as 第一个ViewModel;
}
else if ((string)cmbx_mac.SelectedItem == "另一项")
{
this.VM.2Vm = (this.frameChild.Content as UserControl).DataContext as 第二个ViewModel;
}
}
4、 <ComboBox x:Name="cmbx_mac" ItemsSource="{Binding A}" SelectedItem="{Binding B}"
SelectionChanged="SelectionChanged_page"/>
private void SelectionChanged_page(object sender, SelectionChangedEventArgs e)
{
if ((string)cmbx_mac.SelectedItem == "某项")
{
this.frameChild.Source = new Uri("1.xaml", UriKind.Relative);
}
else if ((string)cmbx_mac.SelectedItem == "另一项")
{
this.frameChild.Source = new Uri("2.xaml", UriKind.Relative);
}
}
5、MainViewModel里
public bool hasChanged { get; set; }
#region
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChange(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
this.hasChanged = true;
}
}
#endregion
public 第一个ViewModel 1Vm
{
get;
set;
}
public 另一个ViewModel 2Vm
{
get;
set;
}
本文介绍了一个WPF应用程序中如何通过导航框架实现页面切换,并根据不同条件更新视图模型的过程。具体包括:利用Frame控件加载不同页面,通过ComboBox选择项决定页面跳转目标,以及在页面加载完成后获取相应的视图模型。
2377

被折叠的 条评论
为什么被折叠?



