关于焦点回到父窗口 [英] Regarding Focus back to parent window

查看:57
本文介绍了关于焦点回到父窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我有一个弹出控件正在应用程序的第一页上打开,但是当我关闭它时,焦点不会回到父页面上.
如何将焦点放在父页面上.
谁能帮我这个忙.
在此先感谢
Nivedita

Hi All
I have a popup control which is opening up on the first page of application, but when I close it the focus is not going back to the parent page.
How to give the focus to the parent page.
Can any one help me on this.
Thanks in advance
Nivedita

推荐答案

关闭弹出窗口后,在窗体上使用Focus方法.

this.Focus();
Use the Focus method on the form after you close the popup.

this.Focus();


首先,在WPF中没有诸如窗口之间的父子关系之类的概念.但是,可以拥有一个窗口(强烈建议).还有一个主要形式.

现在,在正常情况下,您描述的内容将通过激活先前处于活动状态的窗口而结束.请注意,对于Windows,正确的概念是激活而不是聚焦.焦点是从UIElement继承的.因此,应该不费吹灰之力就不起作用.这意味着您搞砸了一些东西.如果没有一些代码示例来演示您的问题,将很难理解. 注意!一个完整的代码示例,但与您的真实"代码不同.你能提供吗?

同时,请尝试以下操作:

呼叫Activate,而不是Focus.注意帮助页面:此方法仅尝试"聚焦,与激活相同,所以...
如果这不起作用,则可能是您尝试聚焦窗口时无法聚焦或无法激活它. 1)将通话推迟到以后; 2)代替呼叫,使用Dispatcher.BeginInvoke;在代表下,调用Activate;这会将调用放在主应用程序周期的队列中,从而延迟调用.

—SA
First, in WPF there is not such concept as parent-child relationships between windows. However, a window can be owned (highly recommended). There is also a main form.

Now, in normal situation what you describe ends up by activating previously active window. Note, for the windows right concept is activation but not focusing. Focus is inherited from the UIElement. So, what is supposed to work without any effort does not work; it means you screw up something. Hard to understand what without some code sample demonstrating your problem. Attention! A complete code sample, but nothing like your "real" code. Can you provide it?

In the meanwhile, try the following:

Call Activate, not Focus. Pay attention for the help page: this method only "attempts" to focus, same about activate, so…
If this does not work, there is a possibility when you tried to focus a window when it cannot be focused or cannot be activated. 1) Defer the call to later moment; 2) instead of call, use Dispatcher.BeginInvoke; under the delegate, call Activate; this will defer the call by putting it in the queue of the main application cycle.

—SA


请检查以下代码,单击按钮即会打开另一个窗口.关闭弹出窗口后,它将焦点重新设置为HostWindow.

注意:我已经为您的要求提供了解决方案,它不是访问事件处理程序的标准方法,因为WPF提供了更强大的命令绑定功能.

如果要使用命令绑定,则必须将焦点重置逻辑转移到应用程序控制器类.


HostWindow.xaml

Please check below code, it opens another window on click of button. When the popped up window is closed, it reset focus back to HostWindow.

Note: I have provided solution to your requirement, its not the standard way to access eventhandlers as WPF provide much more powerful Command binding feature.

If in case you are using command binding, then focus resetting logic will have to be shifted to your application controller class.


HostWindow.xaml

<Window x:Class="SampleWPF.HostWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="HostWindow" Height="300" Width="300">
    <StackPanel>
        <TextBox>tab to check focus working.</TextBox>
        <Button Click="Button_Click">Click To Popup</Button>
    </StackPanel>
</Window>



代码隐藏文件HostWindow.xaml.cs



Codebehind file HostWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SampleWPF
{
    /// <summary>
    /// Interaction logic for HostWindow.xaml
    /// </summary>
    public partial class HostWindow : Window
    {
        public HostWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            PopupWindow pw = new PopupWindow();
            pw.Closed += new EventHandler(pw_Closed);
            //Note: Show dialog makes pw as popup.
            pw.ShowDialog();
        }

        void pw_Closed(object sender, EventArgs e)
        {
            this.Focus();
        }
    }



   public class PopupWindow : Window
    {
    }


}


这篇关于关于焦点回到父窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆