原文:WPF点击不同界面上的按钮实现界面切换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29844879/article/details/80207782

网上看到的都是在一个界面上使用同一个按钮实现界面切换,这里我来做个不同界面上的不同按钮实现界面切换。如下图:

可以看到右下角有个“查询数据”按钮,当点击查询按钮时切换到另一个界面,如下图:

虽然长得很像,但它们绝不是同一个按钮,当点击返回查询时,将回到前面的界面。

代码如下:

第一个界面叫DataManage,其xmal:

  1. <UserControl x:Class="MyControlLibrarys.DataManage"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d" Margin="10 0"
  7. d:DesignHeight="600" d:DesignWidth="850" Loaded="UserControl_Loaded">
  8. <UserControl.Resources>
  9. <ResourceDictionary>
  10. <ResourceDictionary.MergedDictionaries>
  11. <ResourceDictionary Source="pack://application:,,,/GeneralStyle;component/Themes/Generic.xaml" />
  12. <ResourceDictionary Source="pack://application:,,,/GeneralStyle;component/Themes/DataGrid.xaml" />
  13. </ResourceDictionary.MergedDictionaries>
  14. </ResourceDictionary>
  15. </UserControl.Resources>
  16. <Grid>
  17. <Grid.RowDefinitions>
  18. <RowDefinition Height="auto"/>
  19. <RowDefinition Height="*"/>
  20. <RowDefinition Height="auto"/>
  21. <RowDefinition Height="120"/>
  22. </Grid.RowDefinitions>
  23. <Grid>
  24. <Grid.RowDefinitions>
  25. <RowDefinition Height="auto"/>
  26. <RowDefinition Height="auto"/>
  27. <RowDefinition Height="auto"/>
  28. <RowDefinition Height="auto"/>
  29. </Grid.RowDefinitions>
  30. <Grid.ColumnDefinitions>
  31. <ColumnDefinition Width="3*"/>
  32. <ColumnDefinition Width="4*"/>
  33. <ColumnDefinition Width="4*"/>
  34. <ColumnDefinition Width="*"/>
  35. <ColumnDefinition Width="4*"/>
  36. <ColumnDefinition Width="*"/>
  37. <ColumnDefinition Width="4*"/>
  38. <ColumnDefinition Width="*"/>
  39. <ColumnDefinition Width="*"/>
  40. </Grid.ColumnDefinitions>
  41. <Label Content="查询条件" Grid.Column="0" FontSize="20" VerticalContentAlignment="Bottom" HorizontalContentAlignment="Right"/>
  42. <CheckBox Grid.Row="1" Grid.Column="0" Name="uut_nameChebox" Content="UUT类型" FontSize="16" HorizontalAlignment="Right" Margin="0 10" Unchecked="Check_Unchecked"></CheckBox>
  43. <ComboBox Grid.Row="1" Grid.Column="1" Name="uut_nameCombox" FontSize="14" Margin="10" Background="Transparent" DropDownClosed="Combox_DropDownClosed"/>
  44. <CheckBox Grid.Row="2" Grid.Column="0" Name="uut_kindChebox" Content="UUT型号" FontSize="16" HorizontalAlignment="Right" Margin="0 10" Unchecked="Check_Unchecked"></CheckBox>
  45. <ComboBox Grid.Row="2" Grid.Column="1" Name="uut_kindCombox" FontSize="14" Background="Transparent" Margin="10" DropDownClosed="Combox_DropDownClosed"/>
  46. <CheckBox Grid.Row="1" Grid.Column="2" Name="timeChebox" Content="操作时间" FontSize="16" HorizontalAlignment="Right" Margin="0 10" Unchecked="Check_Unchecked"></CheckBox>
  47. <Label Content="从" Grid.Row="1" Grid.Column="3" FontSize="16" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"></Label>
  48. <DatePicker Grid.Row="1" Grid.Column="4" Name="timeStatCombox" FontSize="14" Background="Transparent" Margin="0 10" CalendarClosed="DatePicker_CalendarClosed"/>
  49. <Label Content="到" Grid.Row="1" Grid.Column="5" FontSize="16" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"></Label>
  50. <DatePicker Grid.Row="1" Grid.Column="6" Name="timeEndCombox" FontSize="14" Background="Transparent" Margin="0 10" CalendarClosed="DatePicker_CalendarClosed"/>
  51. <CheckBox Grid.Row="2" Grid.Column="2" Name="userChebox" Content="操 作 员 " FontSize="16" HorizontalAlignment="Right" Margin="0 10" Unchecked="Check_Unchecked"></CheckBox>
  52. <ComboBox Grid.Row="2" Grid.Column="4" Name="userCombox" FontSize="14" Background="Transparent" Margin="0 10" DropDownClosed="Combox_DropDownClosed"/>
  53. <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="5" Grid.ColumnSpan="4" HorizontalAlignment="Center">
  54. <Button Content="重置" Style="{DynamicResource TeamviwerButtonStyle}" Width="100" Height="35" Margin="20 0" Click="Reset_Click"/>
  55. <Button Content="查询" Style="{DynamicResource TeamviwerButtonStyle}" Width="100" Height="35" Margin="10 0" Click="ConditionQuery_Click"/>
  56. </StackPanel>
  57. <Separator Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="9" Margin="20 5 20 20"/>
  58. </Grid>
  59. <DataGrid Grid.Row="1" ColumnHeaderStyle ="{DynamicResource DataGridColumnHeaderStyle}" Name="dgData" AutoGenerateColumns="False" CanUserAddRows="False"
  60. GridLinesVisibility="All" MouseDoubleClick="dgData_MouseDoubleClick">
  61. <DataGrid.Columns>
  62. <DataGridTextColumn Width="0.5*" Header="序号" ElementStyle="{StaticResource contentCenterStyle}" Binding="{Binding Path=OrderId, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
  63. <DataGridTextColumn Header="测试编号" Width="*" Binding="{Binding Path=id, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden"></DataGridTextColumn>
  64. <DataGridTextColumn Header="UUT" Width="*" Binding="{Binding Path=uut_name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
  65. <DataGridTextColumn Header="UUT型号" Width="*" Binding="{Binding Path=uut_kind, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
  66. <DataGridTextColumn Header="UUT编号" Width="*" Binding="{Binding Path=uut_num, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
  67. <DataGridTextColumn Header="操作员" Width="*" Binding="{Binding Path=user, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
  68. <DataGridTextColumn Header="测试时间" Width="2*" Binding="{Binding Path=time,StringFormat='{}{0:yyy-MM-dd HH:mm:ss}', Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
  69. <DataGridTextColumn Header="备注" Width="*" Binding="{Binding Path=remark, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
  70. </DataGrid.Columns>
  71. </DataGrid>
  72. <StackPanel Grid.Row="2" Name="ClipCtrlStackPanel" Margin="0 0 0 20"></StackPanel>
  73. <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right">
  74. <Button Content="查看数据" Style="{DynamicResource TeamviwerButtonStyle}" Width="120" Height="40" Margin="20 0" Click="CatData_Click"/>
  75. <Button Content="删除数据" Style="{DynamicResource TeamviwerButtonStyle}" Width="120" Height="40" Margin="20 0" Click="DelData_Click"/>
  76. </StackPanel>
  77. </Grid>
  78. </UserControl>

后台代码:

在命名空间下定义个委托:

  1. public delegate void CtrlSwitchHandler(); //定义委托

然后,定义事件,并在点击查看数据时触发事件:

  1. public event CtrlSwitchHandler CtrlSwitchEvent; //定义事件,用于界面切换
  2. /// <summary>
  3. /// 查看数据
  4. /// </summary>
  5. /// <param name="sender"></param>
  6. /// <param name="e"></param>
  7. private void CatData_Click(object sender, RoutedEventArgs e)
  8. {
  9. if (this.dgData.SelectedItem == null)
  10. {
  11. MyDialog md = new MyDialog(1, "未选择需要查看的对象,请选择!");
  12. md.Show();
  13. }
  14. else
  15. {
  16. Result_NumStr = ((DataRowView)this.dgData.SelectedItem).Row["id"].ToString();
  17. if(Result_NumStr != "")
  18. {
  19. //触发事件改变父窗口的值
  20. if (CtrlSwitchEvent != null)
  21. {
  22. CtrlSwitchEvent();
  23. }
  24. }
  25. else
  26. {
  27. MyDialog md = new MyDialog(1, "不允许查看空值!");
  28. md.Show();
  29. }
  30. }
  31. }

返回查询原理一样的,我这里界面名叫CatDataControl,在下面有用到,只是不要再定义委托了,这里就不讲述。

最后用一个UserControl装载这两个控件,也可以直接用mainwindow装载,我这里是项目需要,故使用UserControl控件,实现切换代码如下:

xaml:

  1. <UserControl x:Class="MyControlLibrarys.SwitchInterface"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. d:DesignHeight="300" d:DesignWidth="350" Loaded="SwInterface_Load">
  8. <Grid>
  9. <StackPanel Name="stackPanelData"></StackPanel>
  10. </Grid>
  11. </UserControl>

后台代码:

  1. public partial class SwitchInterface : UserControl
  2. {
  3. public SwitchInterface()
  4. {
  5. InitializeComponent();
  6. }
  7. DataManage dataManage = new DataManage(); //用于切换的用户控件--数据管理控件
  8. private double originalHeight = 0.0;
  9. private void SwInterface_Load(object sender, RoutedEventArgs e)
  10. {
  11. originalHeight = stackPanelData.ActualHeight;
  12. dataManage.CtrlSwitchEvent += new CtrlSwitchHandler(CtrlSwitch_CatData);
  13. LoadStackPanelRight(dataManage);
  14. }
  15. //实现界面切换到查看数据界面
  16. private void CtrlSwitch_CatData()
  17. {
  18. CatDataControl catData = new CatDataControl(dataManage.Result_NumStr); //用于切换的用户控件--查看数据控件
  19. catData.CtrlSwitchEvent += new CtrlSwitchHandler(CtrlSwitch_dataManage);
  20. LoadStackPanelRight(catData);
  21. }
  22. //实现界面切换到数据管理界面
  23. private void CtrlSwitch_dataManage()
  24. {
  25. LoadStackPanelRight(dataManage);
  26. }
  27. //加载用户控件功能函数
  28. private void LoadStackPanelRight(UserControl userControl)
  29. {
  30. stackPanelData.Children.Clear();
  31. stackPanelData.Children.Add(userControl);
  32. }
  33. }

如此即可实现,这里面的代码不是每个都有用,这只是部分代码,但绝对能实现界面切换。

WPF点击不同界面上的按钮实现界面切换的更多相关文章

  1. C# 读取word2003 并且显示在界面上的方法

    1.新建一个windows窗体程序 2. 引入包WinWordControl.dll 3.添加引用 4.引入组件WinWordControl组件 5.主界面上加入按钮 ,opendialog, win ...

  2. Plupload 上传详细讲解,Plupload 多实例上传,Plupload多个上传按钮--推荐使用

    今天帮朋友解决  Plupload  上传的问题,查了很多资料,资料还是挺全的,但是有点零零散散的,故整理好,合并发出来. 本教程包括: Plupload  上传详细讲. Plupload  多实例上 ...

  3. 重复点击主界面(TabBar)按钮刷新界面--点击状态栏回到顶部

    1.监听按钮点击   2.判断是否是点击的同一个按钮(记录上次点击的按钮)   3.当重复点击相同按钮时,需要获取当前按钮对应控制器刷新界面      3.1 判断是否重复点击按钮,代码写在哪里?   ...

  4. UITableViewCell上的按钮点击事件处理

    转自:  http://www.aichengxu.com/view/42871 UITableViewCell上的按钮点击事件处理,有需要的朋友可以参考下. 今天突然做项目的时候,又遇到处理自定义的 ...

  5. wordpress在撰写新文章界面的显示选项按钮点击无反应的解决办法

    原文链接:wordpress在撰写新文章界面的显示选项按钮点击无反应的解决办法 最近升级wordpress之后,发现在文章编辑界面的添加新媒体和可视化按钮点击无反应,如下:  然后就在网上找解决办法, ...

  6. C#判断dataGridView1 点击的是哪一列上的按钮

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { ) { DataGr ...

  7. 解决appium点击软键盘上的搜索按钮

    在执行appium自动化测试的时候,需要点击软件盘上的搜索按钮. 具体操作步骤如下: 前提:需要事先安装搜狗输入法 1.唤醒软件盘,可以封装到一个类里,用到的时候随时调用. import os#调起s ...

  8. WPF中,多key值绑定问题,一个key绑定一个界面上的对象

    问题说明: 当用到dictionary<key,value>来储存数据的时候,有时候需要在界面上绑定一个key来显示value,这时候有两种思路: 一种是写一个自定义的扩展类,类似Bind ...

  9. asp.net中遍历界面上所有控件进行属性设置

    * 使用方法: *  前台页面调用方法,重置:    protected void Reset_Click(object sender, EventArgs e)        {           ...

随机推荐

  1. 菜鸟 学注册机编写之 Android app

    0x00前言 环境及工具: 手机    Nexus 4(己root) 系统版本    Android 5.01 工具    AndroidKiller_V1.2 关于Android平台app注册机的编 ...

  2. RabbitMQ双向发送(接收端有返回RPC模式)

    remote procedure call 服务端 import pika import time connection = pika.BlockingConnection(pika.Connecti ...

  3. eclipse 创建 user library 方法

    1.Window - Preferences - Java - Build Path - User Libraries 2.新建 UserLibraries 3. 4.重复上一步依次添加需要的jar文 ...

  4. [SVN]TortoiseSVN工具培训5─常见问题解决

    1.文件提交时状态异常 产生原因:操作不当 解决方法:使用SVN的clean up命令 2.权限不足 产生原因:当前用户权限不足 解决方法:找配置管理员申请权限 3.网络故障 产生原因:无法访问到配置 ...

  5. centos6.5_64bit-kvm安装部署

    kvm部署安装   目录 kvm部署安装... 1 一.kvm部署... 1 1.关闭selinux和防火墙... 1 2.查看主机是否支持虚拟化... 1 3.安装kvm和其他虚拟化软件包... 1 ...

  6. mysql5.6之前需要账号的安全加固

    mysql5.6之前需要账号的安全加固 从5.7开始就不需要了. delete from mysql.user where user!='root' or host='localhost'; flus ...

  7. 解决Jenkins的错误“The Server rejected the connection: None of the protocols were accepted”

    1. 配置节点,配置好节点后,在节点机上运行已下载文件,双击执行,提示"The Server rejected the connection: None of the protocols w ...

  8. Codeforces Round #347 (Div.2)_B. Rebus

    题目链接:http://codeforces.com/contest/664/problem/B B. Rebus time limit per test 1 second memory limit ...

  9. bpclntcmd一条神奇的命令,解决新安装nbu客户端无法连接的问题 (屡试不爽神命令)

    1. bpclntcmd案例 bpclntcmd -clear_host_cache bpclntcmd – 测试 NetBackup 系统的功能,并在 NetBackup 客户端上启用光纤传输服务 ...

  10. es6-promise.auto.js

    使用sweetalert2的IE浏览器报错,导入文件 链接:https://pan.baidu.com/s/1mOcsN_o8m-7I7Rej1NPkiw 提取码:9xsj