none
通过鼠标获得焦点调用自定义用户控件 RRS feed

  • 问题

  • 原因:我设计了一个WPF项目,在项目中有一个MainWindow.xaml主页,并设计了一个自定义用户控件WindowMainControl.xaml,控件中定义了一个DockPanel工具条,主要目的是通过鼠标在MainWindow.xaml主窗口距顶部0~10的位置获得焦点,调用自定义用户控件(显示用户控件),当鼠标离开距主窗口顶部0~10的位置失去焦点,用户控件自动隐藏。

    问题:我在MainWindow.xaml主页中引用了自定义用户控件,并设计设用户控件的现实与隐藏,但调试效果显示用户控件直接显示,不能实现隐藏功能。

    1.程序设计结构如下图:  

    2.自定义用户控件WindowMainControl.xaml,控件中定义了一个DockPanel代码,设计希望将通过焦点在主页中显示DockPanel控件,自定义用户控件WindowMainControl.xaml代码下:

    <UserControl x:Class="Shanzhizi.Control.WindowMainControl.WindowMainControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 mc:Ignorable="d" d:DesignHeight="35" d:DesignWidth="1354">
        <UserControl.Resources>
            <ResourceDictionary Source="WindowMainControlDictionary.xaml" />
        </UserControl.Resources>
        <Grid >
            <DockPanel Name="MenuDocKpanel"  Style="{StaticResource DockPanelResource}">
                <StackPanel Style="{StaticResource StackPanelLeftResource}" >
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/home.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/stuff.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/TN_psd.JPG" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/album.tif" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/RTF.File.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/PDF.File.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/IExplorer.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/mWorld_green.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/Musica_min.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/mWorld_cd_Yellow.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/applications.png" />
                    </Button>
                </StackPanel>
                <StackPanel Style="{StaticResource StackPanelRightResource}" >
                    <Button Click="MinWindow">
                        <Image Source="/Shanzhizi;component/Control/ico/-.png" />
                    </Button>
                    <Button Click="FullWindow">
                        <Image Source="/Shanzhizi;component/Control/ico/+.png" />
                    </Button>
                    <Button Click="CloseWindow">
                        <Image Source="/Shanzhizi;component/Control/ico/delete.png" />
                    </Button>
                </StackPanel>
            </DockPanel>
        </Grid>
    </UserControl>

    用户控件后台代码如下:

    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.Navigation;
    using System.Windows.Shapes;

    namespace Shanzhizi.Control.WindowMainControl
    {
        /// <summary>
        /// WindowMainControl.xaml 的交互逻辑
        /// </summary>
        public partial class WindowMainControl : UserControl
        {
            public WindowMainControl()
            {
                InitializeComponent();
            }

            private void CloseWindow(object sender, RoutedEventArgs e)
            {
                Application.Current.MainWindow.Close();
            }

            private void FullWindow(object sender, RoutedEventArgs e)
            {
                Application.Current.MainWindow.WindowState= WindowState.Maximized;
            }

            private void MinWindow(object sender, RoutedEventArgs e)
            {
                Application.Current.MainWindow.WindowState = WindowState.Minimized;
            }

        }
    }

    注:后台代码没有问题,在主页中引用也无问题

    3.主页MainWindow.xaml代码如下:

    <Window x:Class="Shanzhizi.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:windowmaincontrol="clr-namespace:Shanzhizi.Control.WindowMainControl"
            Title="私人空间"    Width="1360" Height="740" 
            WindowStartupLocation="CenterScreen"  WindowStyle="None"
            PreviewMouseMove="Window_PreviewMouseMove">

        <Grid >
            <windowmaincontrol:WindowMainControl
                LostFocus="WindowMainControl_LostFocus"
                GotFocus="WindowMainControl_GotFocus"
                ContextMenuOpening="WindowMainControl_ContextMenuOpening"/>
        </Grid>
       
    </Window>

    主页后台代码如下:

    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.Navigation;
    using System.Windows.Shapes;


    //引入控件
    using Shanzhizi.Control.WindowMainControl;

    namespace Shanzhizi
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            //自定义用户控件声明
            WindowMainControl WMC = new WindowMainControl();

            private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                // 通过鼠标焦点获自定义用户控件自动显示与隐藏
                //假设标题栏在0和10之间
                if (e.GetPosition(this).Y >= 0 && e.GetPosition(this).Y < 10)
                {
                    if (WMC.Visibility != Visibility.Visible)
                    {
                        WMC.Visibility = Visibility.Visible;
                    }
                }
            }
            //获得焦点
            private void WindowMainControl_LostFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Collapsed;
            }

            //失去焦点
            private void WindowMainControl_GotFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;
            }
            //窗体事件焦点
            private void WindowMainControl_ContextMenuOpening(object sender, ContextMenuEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;
            }
        }
    }

    调试效果:能正常通过编译,并正常调试;

    本人设计问题考虑:通过鼠标MouseMove获得焦点(焦点取在主页顶部0~10之间)显示自定义用户控件,鼠标离开焦点隐藏用户控件;

    问题分析:

    1.自定义用户控件引入方法错误;

    2.

                PreviewMouseMove="Window_PreviewMouseMove">

                LostFocus="WindowMainControl_LostFocus"
                GotFocus="WindowMainControl_GotFocus"
                ContextMenuOpening="WindowMainControl_ContextMenuOpening"

    鼠标事件应用错误;

    3. private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                // 通过鼠标焦点获自定义用户控件自动显示与隐藏
                //假设标题栏在0和10之间
                if (e.GetPosition(this).Y >= 0 && e.GetPosition(this).Y < 10)
                {
                    if (WMC.Visibility != Visibility.Visible)
                    {
                        WMC.Visibility = Visibility.Visible;
                    }
                }
            }

    未获取焦点、焦点设置错误。

    2012年5月18日 4:08

答案

  • 几个错误。

    1. 你重复创建了多个 WindowMainControl ,其中后台代码创建的并没有加入到你的界面中,所以你永远看不见他的。

    你只需要创建一个在XAML中,然后给他个名字即可:

    <windowmaincontrol:WindowMainControl x:Name="WMC"
                 LostFocus="WindowMainControl_LostFocus"
                 GotFocus="WindowMainControl_GotFocus"
                 ContextMenuOpening="WindowMainControl_ContextMenuOpening"/>
     

    2. 要通过其代码让控件获得焦点,首先这个控件必须是可见的,即 WMC.Visibility = Visibility.Visible, 其次控件的 Focusable = true,然后通过代码 WMC。Foucs() 将焦点移到你的控件上。

    private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
    {
       // 通过鼠标焦点获自定义用户控件自动显示与隐藏
       //假设标题栏在0和10之间
       if (e.GetPosition(this).Y >= 0 && e.GetPosition(this).Y < 10)
       {
           if (WMC.Visibility != Visibility.Visible)
           {
               WMC.Visibility = Visibility.Visible;
           }
           WMC.Focus();
       }
    }


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 粗俗人 2012年5月21日 14:50
    2012年5月21日 9:59
    版主
  • 经过试验“Bob Bao ”的方法实现菜单显示但不能通过鼠标失去焦点隐藏菜单,将上面的语法加上else(WMC.Visibility = Visibility.Collapsed;)即可实现菜单的显示隐藏,else功能是鼠标焦点在0~10外,菜单隐藏。

     private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                // 通过鼠标焦点获自定义用户控件自动显示与隐藏
                //假设标题栏在0和10之间
                if (e.GetPosition(this).Y >= 0 && e.GetPosition(this).Y < 35)
                {
                    if (WMC.Visibility != Visibility.Visible)
                    {
                        WMC.Visibility = Visibility.Visible;
                    }
                    //WMC.Focus();
                }
                else
                {
                    WMC.Visibility = Visibility.Collapsed;
                }
            }

    • 已标记为答案 粗俗人 2012年5月21日 14:47
    2012年5月21日 14:47

全部回复

  • 原因:我设计了一个WPF项目,在项目中有一个MainWindow.xaml主页,并设计了一个自定义用户控件WindowMainControl.xaml,控件中定义了一个DockPanel工具条,主要目的是通过鼠标在MainWindow.xaml主窗口距顶部0~10的位置获得焦点,调用自定义用户控件(显示用户控件),当鼠标离开距主窗口顶部0~10的位置失去焦点,用户控件自动隐藏。

    问题:我在MainWindow.xaml主页中引用了自定义用户控件,并设计设用户控件的现实与隐藏,但调试效果显示用户控件直接显示,不能实现隐藏功能。

    1.程序设计结构如下图:  

    2.自定义用户控件WindowMainControl.xaml,控件中定义了一个DockPanel代码,设计希望将通过焦点在主页中显示DockPanel控件,自定义用户控件WindowMainControl.xaml代码下:

    <UserControl x:Class="Shanzhizi.Control.WindowMainControl.WindowMainControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 mc:Ignorable="d" d:DesignHeight="35" d:DesignWidth="1354">
        <UserControl.Resources>
            <ResourceDictionary Source="WindowMainControlDictionary.xaml" />
        </UserControl.Resources>
        <Grid >
            <DockPanel Name="MenuDocKpanel"  Style="{StaticResource DockPanelResource}">
                <StackPanel Style="{StaticResource StackPanelLeftResource}" >
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/home.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/stuff.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/TN_psd.JPG" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/album.tif" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/RTF.File.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/PDF.File.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/IExplorer.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/mWorld_green.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/Musica_min.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/mWorld_cd_Yellow.png" />
                    </Button>
                    <Button >
                        <Image Source="/Shanzhizi;component/Control/ico/applications.png" />
                    </Button>
                </StackPanel>
                <StackPanel Style="{StaticResource StackPanelRightResource}" >
                    <Button Click="MinWindow">
                        <Image Source="/Shanzhizi;component/Control/ico/-.png" />
                    </Button>
                    <Button Click="FullWindow">
                        <Image Source="/Shanzhizi;component/Control/ico/+.png" />
                    </Button>
                    <Button Click="CloseWindow">
                        <Image Source="/Shanzhizi;component/Control/ico/delete.png" />
                    </Button>
                </StackPanel>
            </DockPanel>
        </Grid>
    </UserControl>

    用户控件后台代码如下:

    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.Navigation;
    using System.Windows.Shapes;

    namespace Shanzhizi.Control.WindowMainControl
    {
        /// <summary>
        /// WindowMainControl.xaml 的交互逻辑
        /// </summary>
        public partial class WindowMainControl : UserControl
        {
            public WindowMainControl()
            {
                InitializeComponent();
            }

            private void CloseWindow(object sender, RoutedEventArgs e)
            {
                Application.Current.MainWindow.Close();
            }

            private void FullWindow(object sender, RoutedEventArgs e)
            {
                Application.Current.MainWindow.WindowState= WindowState.Maximized;
            }

            private void MinWindow(object sender, RoutedEventArgs e)
            {
                Application.Current.MainWindow.WindowState = WindowState.Minimized;
            }

        }
    }

    注:后台代码没有问题,在主页中引用也无问题

    3.主页MainWindow.xaml代码如下:

    <Window x:Class="Shanzhizi.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:windowmaincontrol="clr-namespace:Shanzhizi.Control.WindowMainControl"
            Title="私人空间"    Width="1360" Height="740" 
            WindowStartupLocation="CenterScreen"  WindowStyle="None"
            PreviewMouseMove="Window_PreviewMouseMove">

        <Grid >
            <windowmaincontrol:WindowMainControl
                LostFocus="WindowMainControl_LostFocus"
                GotFocus="WindowMainControl_GotFocus"
                ContextMenuOpening="WindowMainControl_ContextMenuOpening"/>
        </Grid>
       
    </Window>

    主页后台代码如下:

    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.Navigation;
    using System.Windows.Shapes;


    //引入控件
    using Shanzhizi.Control.WindowMainControl;

    namespace Shanzhizi
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            //自定义用户控件声明
            WindowMainControl WMC = new WindowMainControl();

            private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                // 通过鼠标焦点获自定义用户控件自动显示与隐藏
                //假设标题栏在0和10之间
                if (e.GetPosition(this).Y >= 0 && e.GetPosition(this).Y < 10)
                {
                    if (WMC.Visibility != Visibility.Visible)
                    {
                        WMC.Visibility = Visibility.Visible;
                    }
                }
            }
            //获得焦点
            private void WindowMainControl_LostFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Collapsed;
            }

            //失去焦点
            private void WindowMainControl_GotFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;
            }
            //窗体事件焦点
            private void WindowMainControl_ContextMenuOpening(object sender, ContextMenuEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;
            }
        }
    }

    调试效果:能正常通过编译,并正常调试;

    本人设计问题考虑:通过鼠标MouseMove获得焦点(焦点取在主页顶部0~10之间)显示自定义用户控件,鼠标离开焦点隐藏用户控件;

    问题分析:

    1.自定义用户控件引入方法错误;

    2.

                PreviewMouseMove="Window_PreviewMouseMove">

                LostFocus="WindowMainControl_LostFocus"
                GotFocus="WindowMainControl_GotFocus"
                ContextMenuOpening="WindowMainControl_ContextMenuOpening"

    鼠标事件应用错误;

    3. private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                // 通过鼠标焦点获自定义用户控件自动显示与隐藏
                //假设标题栏在0和10之间
                if (e.GetPosition(this).Y >= 0 && e.GetPosition(this).Y < 10)
                {
                    if (WMC.Visibility != Visibility.Visible)
                    {
                        WMC.Visibility = Visibility.Visible;
                    }
                }
            }

    未获取焦点、焦点设置错误。

    2012年5月18日 2:27
  • 几个错误。

    1. 你重复创建了多个 WindowMainControl ,其中后台代码创建的并没有加入到你的界面中,所以你永远看不见他的。

    你只需要创建一个在XAML中,然后给他个名字即可:

    <windowmaincontrol:WindowMainControl x:Name="WMC"
                 LostFocus="WindowMainControl_LostFocus"
                 GotFocus="WindowMainControl_GotFocus"
                 ContextMenuOpening="WindowMainControl_ContextMenuOpening"/>
     

    2. 要通过其代码让控件获得焦点,首先这个控件必须是可见的,即 WMC.Visibility = Visibility.Visible, 其次控件的 Focusable = true,然后通过代码 WMC。Foucs() 将焦点移到你的控件上。

    private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
    {
       // 通过鼠标焦点获自定义用户控件自动显示与隐藏
       //假设标题栏在0和10之间
       if (e.GetPosition(this).Y >= 0 && e.GetPosition(this).Y < 10)
       {
           if (WMC.Visibility != Visibility.Visible)
           {
               WMC.Visibility = Visibility.Visible;
           }
           WMC.Focus();
       }
    }


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 粗俗人 2012年5月21日 14:50
    2012年5月21日 9:59
    版主
  • 菜单到显示到窗口中了,但不能通过鼠标失去焦点隐藏菜单。请指教。

    2012年5月21日 14:14
  • 经过试验“Bob Bao ”的方法实现菜单显示但不能通过鼠标失去焦点隐藏菜单,将上面的语法加上else(WMC.Visibility = Visibility.Collapsed;)即可实现菜单的显示隐藏,else功能是鼠标焦点在0~10外,菜单隐藏。

     private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                // 通过鼠标焦点获自定义用户控件自动显示与隐藏
                //假设标题栏在0和10之间
                if (e.GetPosition(this).Y >= 0 && e.GetPosition(this).Y < 35)
                {
                    if (WMC.Visibility != Visibility.Visible)
                    {
                        WMC.Visibility = Visibility.Visible;
                    }
                    //WMC.Focus();
                }
                else
                {
                    WMC.Visibility = Visibility.Collapsed;
                }
            }

    • 已标记为答案 粗俗人 2012年5月21日 14:47
    2012年5月21日 14:47