一、Main.xaml页面
<Window x:Class="WindowModel.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
Title="MainWindow" Height="800" Width="900" ResizeMode="CanResize">
<Grid>
<StackPanel Orientation="Horizontal">
<ComboBox Width="300" x:Name="cbxCmdId"
ItemsSource="{Binding CmdId}" SelectedItem="{Binding ComoSelectedItem,Mode=TwoWay}"
SelectionChanged="SelectionChanged_page"/>
</StackPanel> //ComBox 下拉
<StackPanel Orientation="Horizontal">
<Frame x:Name="frameChild" Height="200" Source="One.xaml" Navigated="frameChild_Navigated" NavigationUIVisibility="Hidden"/>
</StackPanel> //Frame导航 一开始选择一个页面 One.xaml
</Grid>
</Window>
二、Main.xaml.cs页面
public partial class MainWindow : Window
{
public MainViewModel VM //ViewModel是MainModel用了创建一个对象。
{
get
{
return this.DataContext as MainViewModel;
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel(); //通过这个传递数据
this.frameChild.Loaded += frameChild_Loaded; //开始加载Frame中的子页面
this.Closing += MainWindow_Closing; //主页关闭时执行方法MainWindow_Closing;
}
private void SelectionChanged_page(object sender, SelectionChangedEventArgs e) //ComBox选择项改变时
{
if ((string)cbxCmdId.SelectedItem == "One")
{
this.frameChild.Source = new Uri("One.xaml", UriKind.Relative);
}
elseif .........其他类似
}
private void frameChild_Loaded(object sender, RoutedEventArgs e) //主页面加载完成后必须也得加载子页面,这样子页面才能有数据
{
if ((string)cbxCmdId.SelectedItem == "One")
{
this.VM.OneViewModel = (this.frameChild.Content as UserControl).DataContext as OneViewModel ; //加载的同时,把OneViewModel数据取出来显示。
}
else if 类似。。。。。。。
}
private void frameChild_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
if ((string)cbxCmdId.SelectedItem == "One")
{
this.VM.OneViewModel = (this.frameChild.Content as UserControl).DataContext as OneViewModel ; //加载的同时,把OneViewModel数据取出来显示。
}
else if 类似。。。。。}
}
三、MainViewModel页面
public class MainViewModel:INotifyPropertyChanged //继承:INotifyPropertyChanged
{
/// <summary>
/// 显示一个消息
/// </summary>
#region INotifyPropertyChanged 成员
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
public OneViewModel OneViewModel //获得OneViewModel属性的值,传递
{
get;
set;
}
private ObservableCollection<string> _cmdId = new ObservableCollection<string>(); //放下拉列表值
public ObservableCollection<string> CmdId
{
get { return _cmdId; }
set
{
this._cmdId = value;
NotifyPropertyChanged("CmdId");
}
}
private string _comoSelectedItem; //切换时选择哪个
public string ComoSelectedItem
{
get
{ return this._comoSelectedItem; }
set
{
this._comoSelectedItem = value;
NotifyPropertyChanged("ComoSelectedItem");
}
}
public MainWindowModel()
{
_cmdId.Add("值1");
_cmdId.Add("值2");
_cmdId.Add("值3");
_cmdId.Add("值4");
ComoSelectedItem = _cmdId[0]; //默认选择第一个
}
}
四、OneViewModel页面
public class TieXieContainerViewModel :INotifyPropertyChanged //继承:INotifyPropertyChanged
{
private string _user;
public string User //Frame子页面某个属性
{
get { return _user; }
set
{
_user= value;
NotifyPropertyChanged("User");
}
}
public OneViewModel()
{
User = '"IT_ziliang"
}
}
五、One.xaml页面。。。。。frame一个子页面
<UserControl x:Class="WindowModel.One"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="800">
<Grid Width="800">
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Top">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
<TextBlock Width="100" Text=" User:"/>
<TextBox Width="400" Height="20" Text="{Binding User,Mode=TwoWay}"/>
</StackPanel>
</Grid>
</UserControl>
六、One.xaml.cs页面
public partial class One: UserControl
{
public One()
{
InitializeComponent();
this.DataContext = new OneViewModel();
}
}
到此已经可以实现ComBox选择的改变Frame子页面的相应改变。