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 ...
随机推荐
- HDU-4315 Climbing the Hill
题目链接 先回到阶梯博弈的裸题中,比如POJ-1704,所有的块只能向左移并且不能跨越,这个向左移的结果我们可以理解为将左边的宽度减少使得右边的宽度增加,等同于阶梯模型中将石子从高阶移动到低阶.那么最 ...
- AcWing 238.银河英雄传说 (边带权并查集)
题意:有\(n\)列,有\(T\)条指令,若指令格式为\(M\),则将第\(i\)号的所有战舰移到第\(j\)号所在列的后面,若指令格式为\(C\),询问\(i\)和\(j\)是否在同一列,如果在,问 ...
- Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final) C. The Delivery Dilemma (贪心,结构体排序)
题意:你要买\(n\)份午饭,你可以选择自己去买,或者叫外卖,每份午饭\(i\)自己去买需要消耗时间\(b_i\),叫外卖需要\(a_i\),外卖可以同时送,自己只能买完一份后回家再去买下一份,问最少 ...
- centos 7下设置.net core项目开机自启动
1.在etc/systemd/system下创建xxx.service文件 例如:vi /etc/systemd/system/ubif.service2.编辑 ubif.service内容如下: [ ...
- 【cpp上】课后正误小题
State whether each of the following is true or false. If false, explain why. Assume the state ment u ...
- meterpreter php payload && windows payload 学习
一 情景 本地kali linux 192.168.1.2 目标 windows NT 服务器192.168.1.4 目的是获取shell 二 过程 首先在linux建立终端 ,msfconsole ...
- Gym 101170F Free Weights(二分)题解
题意:给出两行,每一行都有n个数组,一共有2 * n个,大小为1~n,每个有两个.现在可以进行操作:拿出一个物品i,然后放到一个空格,花费i.可以任意平移物品,平移没有花费.每一行空间无限.要求你把一 ...
- js sort tricks All In One
js sort tricks All In One js 排序技巧 const arr = [ { label: 'False 1 ', disabled: false, }, { label: 'F ...
- 浏览器缓存 All In One
浏览器缓存 All In One HTTP 缓存 强缓存 expired Cache-Control max-age s-maxage 协商缓存 E-tag last-modified 本地缓存 co ...
- Google Developer Days 2019 & GDD
Google Developer Days 2019 2019 Google 开发者大会 GDD Google Developer Days https://events.google.cn/intl ...