框架使用的是MVVM
View层 点击combobox后会有9个选项,每个选项会对应不同的Page,如何实现
第一步:绑定combobox的里面的数据,然后下面给个Frame view端此截图代码
<StackPanel Grid.Row="1" Grid.Column="1"> <GroupBox Header="测试项目" Height="652" Width="569" Margin="5"> <Grid> <StackPanel Orientation="Horizontal" Height="50" Grid.Row="0"> <Label Content="测试项目:" Height="50" FontSize="30"/> <ComboBox Width="350" ItemsSource="{Binding TestItemList}" SelectedItem="{Binding ItemIsSelect}" FontSize="25"></ComboBox> </StackPanel> <Frame x:Name="EditScriptPage" Source="{Binding FramePage}" Grid.Row="1" Background="#F5F6FA" NavigationUIVisibility="Hidden" /> <Grid.RowDefinitions> <RowDefinition Height="50"/> <RowDefinition Height="522"/> </Grid.RowDefinitions> </Grid> </GroupBox> </StackPanel>2.ViewModel里的实现代码
/// <summary> /// combobox数据 /// </summary> public ObservableCollection<string> TestItemList { get; set; } public RhEditScriptViewModel() { TestItemList = new ObservableCollection<string>(); TestItemList.Add("Relay"); TestItemList.Add("SetPower_CurrentModel"); TestItemList.Add("SetPower_VoltageModel"); TestItemList.Add("SetPower"); TestItemList.Add("SetVoltage"); TestItemList.Add("SetCurrent"); TestItemList.Add("Delay_ms"); TestItemList.Add("ReadVoltage"); TestItemList.Add("ReadCurrent"); //初始化combobox第一条数据显示和相对应的第一个page页面 if (_ItemIsSelect == null) _ItemIsSelect = "Relay"; if (FramePage == null) FramePage= new Uri("RelayEditScript.xaml", UriKind.Relative); } /// <summary> /// 导航页面地址 /// </summary> public Uri FramePage { get; set; } private string _ItemIsSelect; /// <summary> /// combobox数据绑定 /// </summary> public string ItemIsSelect { get { return _ItemIsSelect; } set { _ItemIsSelect = value; //根据combobox的数据转换Page if (_ItemIsSelect.Equals("Relay")) { FramePage = new Uri("RelayEditScript.xaml", UriKind.Relative); } else if (_ItemIsSelect.Equals("SetPower_CurrentModel")) { FramePage = new Uri("SetPower_CurrentModelEditScript.xaml", UriKind.Relative); } else if (_ItemIsSelect.Equals("SetPower_VoltageModel")) { FramePage = new Uri("SetPower_VoltageModelEditScript.xaml", UriKind.Relative); } else if (_ItemIsSelect.Equals("SetPower")) { FramePage = new Uri("SetPowerEditScript.xaml", UriKind.Relative); } else if (_ItemIsSelect.Equals("SetPower")) { FramePage = new Uri("SetPowerEditScript.xaml", UriKind.Relative); } else if (_ItemIsSelect.Equals("SetVoltage")) { FramePage = new Uri("SetVoltageEditScript.xaml", UriKind.Relative); }else if (_ItemIsSelect.Equals("SetCurrent")){ FramePage = new Uri("SetCurrentEditScript.xaml", UriKind.Relative); }else if (_ItemIsSelect.Equals("Delay_ms")){ FramePage = new Uri("Delay_msEditScript.xaml", UriKind.Relative); }else if (_ItemIsSelect.Equals("ReadVoltage")){ FramePage = new Uri("ReadVoltageEditScript.xaml", UriKind.Relative); }else if (_ItemIsSelect.Equals("ReadCurrent")){ FramePage = new Uri("ReadCurrentEditScript.xaml", UriKind.Relative); } RaisePropertyChanged("ItemIsSelect"); RaisePropertyChanged("FramePage"); } }实现后 界面会随着combobox选项的改变而变化
