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;
}