WPF UserControl组合的用户界面,关闭窗口后,焦点问题

在使用UserControl组合的用户界面,模式采用的是MVVM模式,分离界面和逻辑设计,

其中发现一个问题:点击某个组合控件里面的某个图标,弹出窗口,关闭窗口后,焦点没有回到主界面,导致主界面的快捷键功能不触发问题

原因:关闭弹出窗口后,焦点失去,主界面没有获取到焦点;

解决:

1.获取父类对象帮助类

public static class  ControlHelper
{
    public static T GetParentObject<T>(DependencyObject obj) where T:FrameworkElement

    {
        if (obj == null)
            return default(T);
        DependencyObject parent = VisualTreeHelper.GetParent(obj);//利用VisualTreeHelper寻找指定依赖对象的父级对象
        while (parent != null)
        {
            if (parent is T)
                return (T)parent;
            parent = VisualTreeHelper.GetParent(parent);
        }
        return default(T);
    }
}

2.调用实例:本例是 ListBox为父级对象

pprivate void LableComm()
{
    DependencyObject element = Mouse.DirectlyOver as DependencyObject;
    System.Windows.Controls.ListBox parent =         (System.Windows.Controls.ListBox)ControlHelper.GetParentObject<System.Windows.Controls.L    istBox>(element);
    //弹出窗口调用
    var dlg = new LableDialogView();
    bool isConfirm = (bool)dlg.ShowDialog();
    //焦点重新获取
    if (parent != null)
        parent.Focus();
}

3.快捷键相应函数中,执行完操作后,要把控件获取焦点(后台逻辑)

 <ListBox x:Name="ImageListBox" Grid.Column="1" Background="Transparent" BorderThickness="0" />
    例如:ListBox为自定义的模板控件
    void ImageListViewItem_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        //快捷键事件处理
        
        //处理结束
        ImageListBox.Focus();
    }

4.主界面快捷键函数响应,实现

        private void EditReportView_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.F5 || e.Key == Key.F6 || e.Key == Key.F7 || e.Key == Key.F8 || e.Key == Key.Delete)
                {
                    if (e.OriginalSource is ListBox)
                    {
                        var control = e.OriginalSource as ListBox;
                        if (control.Name == "CaptureImageListBox" || control.Name == "PrintedImageListBox")
                        {
                            var data= control.SelectedItem as ImageInfo;
                            if (data == null || m_VM == null)
                                return;
                            e.Handled = true;
                            if (e.Key == Key.F5)
                            {  
                            }
                            else if (e.Key == Key.F6)
                            { 
                            }
                            else if (e.Key == Key.F7)
                            {
                            }
                            else if (e.Key == Key.F8)
                            { 
                            }
                            else if (e.Key == Key.Delete)
                            {
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

 


版权声明:本文为BYH371256原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。