原文:通过WPF中UserControl内的按钮点击关闭父窗体

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

通过WPF中UserControl内的按钮点击关闭父窗体

1.目的:

在设计界面的过程中,想通过UserControl内的一个按钮点击来关闭包含UserControl的父窗体,来展示其他的界面。

2.实现思路:

2.1我们知道在WPF中的UserControl,他本身是没有this.close事件的:



2.2能否找到UserControl的父容器来关闭



或许是我没找到,没有找到能关闭父容器的方法

3.实现方法:

最后通过Stack Overflow终于找到了可以实现该功能的方法:

 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
private static extern IntPtr GetParent(IntPtr hWnd); //I'd double check this constant, just in case
static uint WM_CLOSE = 0x10; private void CloseContainingWindow(Visual visual)
{
// Find the containing HWND for the Visual in question
HwndSource wpfHandle = PresentationSource.FromVisual(this) as HwndSource;
if (wpfHandle == null)
{
throw new Exception("Could not find Window handle");
} // Trace up the window chain, to find the ultimate parent
IntPtr hWindow = wpfHandle.Handle;
while (true)
{
IntPtr parentHWindow = GetParent(hWindow);
if (parentHWindow == (IntPtr)0) break;
hWindow = parentHWindow;
} // Now send the containing window a close message
SendMessage(hWindow, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}

将我们的UseControl传入到该方法:



4.参考链接

MFC-hosted WPF usercontrol how to close parent window on button press

通过WPF中UserControl内的按钮点击关闭父窗体的更多相关文章

  1. 在WPF中UserControl

    在这里我们将将打造一个UserControl(用户控件)来逐步讲解如何在WPF中自定义控件,并将WPF的一些新特性引入到自定义控件中来.我们制作了一个带语音报时功能的钟表控件, 效果如下: 在VS中右 ...

  2. WPF中UserControl和DataTemplate

    最新更新: http://denghejun.github.io 前言 前言总是留给我说一些无关主题的言论,WPF作为全新Microsoft桌面或web应用程序显示技术框架, 从08年开始,一直到现在 ...

  3. WPF 中 UserControl作为另一个Process宿主到Window里, ErrorTemplate的默认红框没有出现

    最近做WPF项目遇到一个问题, 我有2个process, 一个Process里只有Usercontrol, 另一个Process获取前一个Process中Usercontrol并host到当前的win ...

  4. WPF中C#代码触发鼠标点击事件

    1.如下代码; <Button x:Name="btnTest" Click="btnTest_Click"> <Button.Trigger ...

  5. wpf中UserControl的几种绑定方式

    我们经常会抽取一些可重用的控件,某个属性是否需要重用,直接决定了这个属性的绑定方式. 1.完全不可重用的控件 有一些与业务强相关的控件,它们的属性完全来自ViewModel,越是相对复杂的控件,越容易 ...

  6. wpf的UserControl用户控件怎么添加到Window窗体中

    转载自 http://www.cnblogs.com/shuang121/archive/2013/01/09/2853591.html 我们来新建一个用户控件UserControl1.xaml &l ...

  7. ASPxGridView中Command列自定义按钮点击事件概要

    其中CustomButtonClick="ButtonClick",e.buttonID可以获取到自定义按钮的id e.visibleIndex获取到行的索引 grdList.Ge ...

  8. vue 使用element-ui中的Notification自定义按钮并实现关闭功能以及如何处理多个通知

    使用element-ui中的Notification,只有一个message属性是有很大的操作空间,其余的都是写死的,无法进行扩展,达不到想要的效果.所以只能在message上下功夫. 在elemen ...

  9. WPF中,输入完密码回车提交 ,回车触发按钮点击事件

    类似与winform中窗体的AcceptButton属性,在wpf中,需要将按钮的IsDefault设置为true就行.

随机推荐

  1. 【hdu 4289】Control

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=4289 [Description] 给出一个又n个点,m条边组成的无向图.给出两个点s,t.对于图中 ...

  2. SpringMVC &amp; Struts2

    这两个框架可谓Java中的经典,Java开发必懂的框架,这两天在面试中又问道两者的异同.这里简单做了整理供大家參考交流. 概念:

  3. [Angular] Create a custom validator for template driven forms in Angular

    User input validation is a core part of creating proper HTML forms. Form validators not only help yo ...

  4. worktools-mmx 添加编译模块

    1,添加模块到mmx.py文件 1>vim /home/zhangshuli/git/vanzo_team/xulei/mmx.py 2>按照格式"Gallery2": ...

  5. [BZOJ1672][Usaco2005 Dec]Cleaning Shifts 清理牛棚 线段树优化DP

    链接 题意:给你一些区间,每个区间都有一个花费,求覆盖区间 \([S,T]\) 的最小花费 题解 先将区间排序 设 \(f[i]\) 表示决策到第 \(i\) 个区间,覆盖满 \(S\dots R[i ...

  6. 1.26 Python知识进阶 - 继承

    继承 继承(Inheritance)是面向对象的程序设计中代码重要的主要方法.继承是允许使用现有类的功能,并在无需重新改写原来的类的情况下,对这些功能进行扩展.继承可以避免代码复制和相关的代码维护等问 ...

  7. THC=TERMINAL HANDLING CHARGE,碼頭操作費

    THC=TERMINAL HANDLING CHARGE,碼頭操作費

  8. python3操作Excel

    1.安装openpyxl模块: 在cmd命令窗执行命令 pip install openpyxl    安装openpyxl模块 from openpyxl import load_workbook ...

  9. 【Educational Codeforces Round 35 B】Two Cakes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从小到大枚举x. 看看a/x+b/x是不是大于等于n 是的话. 看看是不是两种蛋糕都能凑一堆. 也即x的最大枚举量是min(a,b) ...

  10. 怎样 TabHostFragment自己定义 tab键(indicator)

    1 获得 tabHostFragment: ActionBarActivity activity2 = (ActionBarActivity) activity; mTabHost = new Fra ...