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

通过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

在Windows Presentation Foundation (WPF),为了实现在UserControl的某个属性(通常称为“Propp”)随其包含的窗口一起移动,你需要使用依赖属性(DependencyProperty)以及Layout系统。以下是一个简单的步骤: 1. 首先,在UserControl定义一个依赖属性,它代表你要跟踪的位置或其他需要移动的状态。例如: ```csharp public static readonly DependencyProperty PositionProperty = DependencyProperty.Register("Position", typeof(Point), typeof(MyUserControl), new FrameworkPropertyMetadata(new Point(), OnPositionChanged)); private Point position; public Point Position { get { return position; } set { position = value; UpdateLayout(); OnPropertyChanged(PositionProperty); } } protected virtual void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { // 触发值变化通知 } ``` 2. 在`OnPositionChanged`委托,当依赖属性值改变时,更新UI布局: ```csharp private static void OnPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = (MyUserControl)d; control.UpdateLayout(); } private void UpdateLayout() { // 根据新的位置调整控件的位置或大小 this.LayoutUpdated += MyUserControl_LayoutUpdated; this.Arrange(new Rect(this.Position, Size)); this.LayoutUpdated -= MyUserControl_LayoutUpdated; } ``` 3. 当窗口布局发生更改时(比如窗口大小调整或位置变化),需要重写`LayoutUpdated`事件处理程序,确保控件的位置也随之更新: ```csharp private void MyUserControl_LayoutUpdated(object sender, EventArgs e) { // 更新控件的实际位置 // 这里可以使用GetLeft()、GetTop()等方法获取布局后的坐标 Position = new Point(GetLeft(), GetTop()); } ``` 4. 要使这个UserControl能够添加到任意地方并且随着窗口移动,记得在窗口上设置`DockPanel.Dock`或`Grid.Column/Row`等,以便让UserControl动态适应布局。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值