WPF窗口和用户控件事件相互触发
问题1:
WPF项目里有一个窗口和一个用户控件,窗口和用户控件里都有一个Button,点击窗口里的Button如何触发用户控件里Button的Click事件
解答:
//窗口代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
contentControl1.Content = new UserControl1();
} private void 窗口Button_Click(object sender, RoutedEventArgs e)
{
//如何触发 用户控件Button_Click
var v = contentControl1.Content as UserControl1;
v.DoSomethings();
}
}
//用户控件代码
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
} private void 用户控件Button_Click(object sender, RoutedEventArgs e)
{
DoSomethings();
} public void DoSomethings()
{
用户控件Button.Content = "Hello World";
}
}
问题2:
WPF项目里有一个窗口和一个用户控件,窗口和用户控件里都有一个Button,点击用户控件里的Button如何触发窗体里Button的Click事件
解答:
//用户控件代码
private void 用户控件Button_Click(object sender, RoutedEventArgs e)
{
var w = Window.GetWindow(用户控件Button); // 获取当前窗体
var b = w.FindName("okButton") as Button; // 找到当前窗体上的 okButton
b.RaiseEvent(new RoutedEventArgs(e.RoutedEvent));
}
WPF窗口和用户控件事件相互触发的更多相关文章
- WPF 精修篇 用户控件
原文:WPF 精修篇 用户控件 增加用户控件 数据绑定还是用依赖属性 使用的事件 就委托注册一下 public delegate void ButtonClick(object b,EventArgs ...
- WPF之路——用户控件对比自定义控件UserControl VS CustomControl)
将多个现有的控件组合成一个可重用的“组”. 由一个XAML文件和一个后台代码文件. 不能使用样式和模板. 继承自UserControl类. 自定义控件(扩展) 在现有的控件上进行扩展,增加一些新的属性 ...
- WPF学习- AllowDrop 用户控件启用拖放功能
知识点: 创建自定义用户控件(UserControl) 使用户控件成为拖动源 使用户控件成为放置目标 使面板能够接收从用户控件放置的数据 创建项目: 1.新建WPF项目(Wpf-AllowDrop) ...
- ASP.NET用户控件事件的定义和实践
假定用户控件(UserControl.ascx)中包含按钮控件 AButton,希望实现按 Button 按钮时,包含该用户控件的页面可以接收到事件. UserControl.ascx.cs ...
- wpf的UserControl用户控件怎么添加到Window窗体中
转载自 http://www.cnblogs.com/shuang121/archive/2013/01/09/2853591.html 我们来新建一个用户控件UserControl1.xaml &l ...
- [WPF 学习] 3.用户控件库使用资源字典的困惑
项目需要(或者前后端分离的需要),前端我使用了用户控件库,由后端用代码加载和控制. 然而用户控件库没法指定资源字典,于是在用户控件的xaml文件里面手工添加了资源字典 <UserControl. ...
- WPF获取当前用户控件的父级窗体
方式一.通过当前控件名获取父级窗体 Window targetWindow = Window.GetWindow(button); 方式二.通过当前控件获取父级窗体 Window parentWind ...
- VS2015创建类库项目后添加不了WPF资源字典,窗口,用户控件处理办法
打开项目工程文件在PropertyGroup标签最后加上下面3行: <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FA ...
- winform 用户控件事件的写法
public partial class UcTest : UserControl { public UcTest() { InitializeComponent(); } //定义事件 public ...
随机推荐
- Codeforces Round #643 (Div. 2) 题解 (ABCDE)
目录 A. Sequence with Digits B. Young Explorers C. Count Triangles D. Game With Array E. Restorer Dist ...
- HDU 6852 Increasing and Decreasing 构造
题意: 给你一个n,x,y.你需要找出来一个长度为n的序列,使得这个序列满足最长上升子序列长度为x,最长下降子序列长度为y.且这个序列中每个数字只能出现一次 且要保证最后输出的序列的字典序最小 题解: ...
- Musical Theme POJ - 1743 后缀数组
A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the ...
- SpringBoot引入openfeign 报错:spring-cloud-starter-openfeign:unknown
现象: 1.maven报错:Cannot resolve org.springframework.cloud:spring-cloud-starter-openfeign:unknown 解决: 在h ...
- Python——requests模块
一.安装模块 pip install requests 二.引用 import requests 三.get方法 #GET访问页面 r = requests.get(url) print(r.text ...
- Shell 函数 & 数组
Shell 函数 函数介绍 # 什么是函数? 具备某一功能的工具 => 函数 事先准备工具的过程 => 函数的定义 遇到应用场景拿来就用 => 函数的调用 # 为何要用函数? 没有引 ...
- MongoDB 副本集搭建 & 副本集扩容
副本集的搭建 创建多实例目录 [root@redis03 ~]# mkdir /server/mongodb/2801{7,8,9}/{conf,logs,pid,data} -p 编辑多实例配置文件 ...
- Gitlab 快速部署及日常维护 (一)
一.GitLab简介GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务 二.GitLab系统架构git用户的主目录通常是/home/git(~ ...
- git命令简写配置
在使用git工具时,有些命令比较常用,为了加快输入速度,可以自定义一些简写配置,如下所示: git st # git status git ci # git commit git br # git b ...
- TCP协议与UDP协议的区别以及与TCP/IP协议的联系
先介绍下什么是TCP,什么是UDP. 1. 什么是TCP? TCP(Transmission Control Protocol,传输控制协议)是面向连接的.可靠的字节流服务,也就是说,在收发数据前,必 ...