WPF点击不同界面上的按钮实现界面切换
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29844879/article/details/80207782
网上看到的都是在一个界面上使用同一个按钮实现界面切换,这里我来做个不同界面上的不同按钮实现界面切换。如下图:
可以看到右下角有个“查询数据”按钮,当点击查询按钮时切换到另一个界面,如下图:
虽然长得很像,但它们绝不是同一个按钮,当点击返回查询时,将回到前面的界面。
代码如下:
第一个界面叫DataManage,其xmal:
<UserControl x:Class="MyControlLibrarys.DataManage"
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" Margin="10 0"
d:DesignHeight="600" d:DesignWidth="850" Loaded="UserControl_Loaded">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/GeneralStyle;component/Themes/Generic.xaml" />
<ResourceDictionary Source="pack://application:,,,/GeneralStyle;component/Themes/DataGrid.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="120"/>
</Grid.RowDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Content="查询条件" Grid.Column="0" FontSize="20" VerticalContentAlignment="Bottom" HorizontalContentAlignment="Right"/>
<CheckBox Grid.Row="1" Grid.Column="0" Name="uut_nameChebox" Content="UUT类型" FontSize="16" HorizontalAlignment="Right" Margin="0 10" Unchecked="Check_Unchecked"></CheckBox>
<ComboBox Grid.Row="1" Grid.Column="1" Name="uut_nameCombox" FontSize="14" Margin="10" Background="Transparent" DropDownClosed="Combox_DropDownClosed"/>
<CheckBox Grid.Row="2" Grid.Column="0" Name="uut_kindChebox" Content="UUT型号" FontSize="16" HorizontalAlignment="Right" Margin="0 10" Unchecked="Check_Unchecked"></CheckBox>
<ComboBox Grid.Row="2" Grid.Column="1" Name="uut_kindCombox" FontSize="14" Background="Transparent" Margin="10" DropDownClosed="Combox_DropDownClosed"/>
<CheckBox Grid.Row="1" Grid.Column="2" Name="timeChebox" Content="操作时间" FontSize="16" HorizontalAlignment="Right" Margin="0 10" Unchecked="Check_Unchecked"></CheckBox>
<Label Content="从" Grid.Row="1" Grid.Column="3" FontSize="16" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"></Label>
<DatePicker Grid.Row="1" Grid.Column="4" Name="timeStatCombox" FontSize="14" Background="Transparent" Margin="0 10" CalendarClosed="DatePicker_CalendarClosed"/>
<Label Content="到" Grid.Row="1" Grid.Column="5" FontSize="16" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"></Label>
<DatePicker Grid.Row="1" Grid.Column="6" Name="timeEndCombox" FontSize="14" Background="Transparent" Margin="0 10" CalendarClosed="DatePicker_CalendarClosed"/>
<CheckBox Grid.Row="2" Grid.Column="2" Name="userChebox" Content="操 作 员 " FontSize="16" HorizontalAlignment="Right" Margin="0 10" Unchecked="Check_Unchecked"></CheckBox>
<ComboBox Grid.Row="2" Grid.Column="4" Name="userCombox" FontSize="14" Background="Transparent" Margin="0 10" DropDownClosed="Combox_DropDownClosed"/>
<StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="5" Grid.ColumnSpan="4" HorizontalAlignment="Center">
<Button Content="重置" Style="{DynamicResource TeamviwerButtonStyle}" Width="100" Height="35" Margin="20 0" Click="Reset_Click"/>
<Button Content="查询" Style="{DynamicResource TeamviwerButtonStyle}" Width="100" Height="35" Margin="10 0" Click="ConditionQuery_Click"/>
</StackPanel>
<Separator Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="9" Margin="20 5 20 20"/>
</Grid>
<DataGrid Grid.Row="1" ColumnHeaderStyle ="{DynamicResource DataGridColumnHeaderStyle}" Name="dgData" AutoGenerateColumns="False" CanUserAddRows="False"
GridLinesVisibility="All" MouseDoubleClick="dgData_MouseDoubleClick">
<DataGrid.Columns>
<DataGridTextColumn Width="0.5*" Header="序号" ElementStyle="{StaticResource contentCenterStyle}" Binding="{Binding Path=OrderId, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
<DataGridTextColumn Header="测试编号" Width="*" Binding="{Binding Path=id, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden"></DataGridTextColumn>
<DataGridTextColumn Header="UUT" Width="*" Binding="{Binding Path=uut_name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
<DataGridTextColumn Header="UUT型号" Width="*" Binding="{Binding Path=uut_kind, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
<DataGridTextColumn Header="UUT编号" Width="*" Binding="{Binding Path=uut_num, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
<DataGridTextColumn Header="操作员" Width="*" Binding="{Binding Path=user, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
<DataGridTextColumn Header="测试时间" Width="2*" Binding="{Binding Path=time,StringFormat='{}{0:yyy-MM-dd HH:mm:ss}', Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
<DataGridTextColumn Header="备注" Width="*" Binding="{Binding Path=remark, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Grid.Row="2" Name="ClipCtrlStackPanel" Margin="0 0 0 20"></StackPanel>
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="查看数据" Style="{DynamicResource TeamviwerButtonStyle}" Width="120" Height="40" Margin="20 0" Click="CatData_Click"/>
<Button Content="删除数据" Style="{DynamicResource TeamviwerButtonStyle}" Width="120" Height="40" Margin="20 0" Click="DelData_Click"/>
</StackPanel>
</Grid>
</UserControl>
后台代码:
在命名空间下定义个委托:
public delegate void CtrlSwitchHandler(); //定义委托
然后,定义事件,并在点击查看数据时触发事件:
public event CtrlSwitchHandler CtrlSwitchEvent; //定义事件,用于界面切换
/// <summary>
/// 查看数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CatData_Click(object sender, RoutedEventArgs e)
{
if (this.dgData.SelectedItem == null)
{
MyDialog md = new MyDialog(1, "未选择需要查看的对象,请选择!");
md.Show();
}
else
{
Result_NumStr = ((DataRowView)this.dgData.SelectedItem).Row["id"].ToString();
if(Result_NumStr != "")
{
//触发事件改变父窗口的值
if (CtrlSwitchEvent != null)
{
CtrlSwitchEvent();
}
}
else
{
MyDialog md = new MyDialog(1, "不允许查看空值!");
md.Show();
}
}
}
返回查询原理一样的,我这里界面名叫CatDataControl,在下面有用到,只是不要再定义委托了,这里就不讲述。
最后用一个UserControl装载这两个控件,也可以直接用mainwindow装载,我这里是项目需要,故使用UserControl控件,实现切换代码如下:
xaml:
<UserControl x:Class="MyControlLibrarys.SwitchInterface"
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="350" Loaded="SwInterface_Load">
<Grid>
<StackPanel Name="stackPanelData"></StackPanel>
</Grid>
</UserControl>
后台代码:
public partial class SwitchInterface : UserControl
{
public SwitchInterface()
{
InitializeComponent();
}
DataManage dataManage = new DataManage(); //用于切换的用户控件--数据管理控件
private double originalHeight = 0.0;
private void SwInterface_Load(object sender, RoutedEventArgs e)
{
originalHeight = stackPanelData.ActualHeight;
dataManage.CtrlSwitchEvent += new CtrlSwitchHandler(CtrlSwitch_CatData);
LoadStackPanelRight(dataManage);
}
//实现界面切换到查看数据界面
private void CtrlSwitch_CatData()
{
CatDataControl catData = new CatDataControl(dataManage.Result_NumStr); //用于切换的用户控件--查看数据控件
catData.CtrlSwitchEvent += new CtrlSwitchHandler(CtrlSwitch_dataManage);
LoadStackPanelRight(catData);
}
//实现界面切换到数据管理界面
private void CtrlSwitch_dataManage()
{
LoadStackPanelRight(dataManage);
}
//加载用户控件功能函数
private void LoadStackPanelRight(UserControl userControl)
{
stackPanelData.Children.Clear();
stackPanelData.Children.Add(userControl);
}
}
如此即可实现,这里面的代码不是每个都有用,这只是部分代码,但绝对能实现界面切换。
WPF点击不同界面上的按钮实现界面切换的更多相关文章
- C# 读取word2003 并且显示在界面上的方法
1.新建一个windows窗体程序 2. 引入包WinWordControl.dll 3.添加引用 4.引入组件WinWordControl组件 5.主界面上加入按钮 ,opendialog, win ...
- Plupload 上传详细讲解,Plupload 多实例上传,Plupload多个上传按钮--推荐使用
今天帮朋友解决 Plupload 上传的问题,查了很多资料,资料还是挺全的,但是有点零零散散的,故整理好,合并发出来. 本教程包括: Plupload 上传详细讲. Plupload 多实例上 ...
- 重复点击主界面(TabBar)按钮刷新界面--点击状态栏回到顶部
1.监听按钮点击 2.判断是否是点击的同一个按钮(记录上次点击的按钮) 3.当重复点击相同按钮时,需要获取当前按钮对应控制器刷新界面 3.1 判断是否重复点击按钮,代码写在哪里? ...
- UITableViewCell上的按钮点击事件处理
转自: http://www.aichengxu.com/view/42871 UITableViewCell上的按钮点击事件处理,有需要的朋友可以参考下. 今天突然做项目的时候,又遇到处理自定义的 ...
- wordpress在撰写新文章界面的显示选项按钮点击无反应的解决办法
原文链接:wordpress在撰写新文章界面的显示选项按钮点击无反应的解决办法 最近升级wordpress之后,发现在文章编辑界面的添加新媒体和可视化按钮点击无反应,如下: 然后就在网上找解决办法, ...
- C#判断dataGridView1 点击的是哪一列上的按钮
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { ) { DataGr ...
- 解决appium点击软键盘上的搜索按钮
在执行appium自动化测试的时候,需要点击软件盘上的搜索按钮. 具体操作步骤如下: 前提:需要事先安装搜狗输入法 1.唤醒软件盘,可以封装到一个类里,用到的时候随时调用. import os#调起s ...
- WPF中,多key值绑定问题,一个key绑定一个界面上的对象
问题说明: 当用到dictionary<key,value>来储存数据的时候,有时候需要在界面上绑定一个key来显示value,这时候有两种思路: 一种是写一个自定义的扩展类,类似Bind ...
- asp.net中遍历界面上所有控件进行属性设置
* 使用方法: * 前台页面调用方法,重置: protected void Reset_Click(object sender, EventArgs e) { ...
随机推荐
- 菜鸟 学注册机编写之 Android app
0x00前言 环境及工具: 手机 Nexus 4(己root) 系统版本 Android 5.01 工具 AndroidKiller_V1.2 关于Android平台app注册机的编 ...
- RabbitMQ双向发送(接收端有返回RPC模式)
remote procedure call 服务端 import pika import time connection = pika.BlockingConnection(pika.Connecti ...
- eclipse 创建 user library 方法
1.Window - Preferences - Java - Build Path - User Libraries 2.新建 UserLibraries 3. 4.重复上一步依次添加需要的jar文 ...
- [SVN]TortoiseSVN工具培训5─常见问题解决
1.文件提交时状态异常 产生原因:操作不当 解决方法:使用SVN的clean up命令 2.权限不足 产生原因:当前用户权限不足 解决方法:找配置管理员申请权限 3.网络故障 产生原因:无法访问到配置 ...
- centos6.5_64bit-kvm安装部署
kvm部署安装 目录 kvm部署安装... 1 一.kvm部署... 1 1.关闭selinux和防火墙... 1 2.查看主机是否支持虚拟化... 1 3.安装kvm和其他虚拟化软件包... 1 ...
- mysql5.6之前需要账号的安全加固
mysql5.6之前需要账号的安全加固 从5.7开始就不需要了. delete from mysql.user where user!='root' or host='localhost'; flus ...
- 解决Jenkins的错误“The Server rejected the connection: None of the protocols were accepted”
1. 配置节点,配置好节点后,在节点机上运行已下载文件,双击执行,提示"The Server rejected the connection: None of the protocols w ...
- Codeforces Round #347 (Div.2)_B. Rebus
题目链接:http://codeforces.com/contest/664/problem/B B. Rebus time limit per test 1 second memory limit ...
- bpclntcmd一条神奇的命令,解决新安装nbu客户端无法连接的问题 (屡试不爽神命令)
1. bpclntcmd案例 bpclntcmd -clear_host_cache bpclntcmd – 测试 NetBackup 系统的功能,并在 NetBackup 客户端上启用光纤传输服务 ...
- es6-promise.auto.js
使用sweetalert2的IE浏览器报错,导入文件 链接:https://pan.baidu.com/s/1mOcsN_o8m-7I7Rej1NPkiw 提取码:9xsj