【问题标题】:WPF FrameworkElement Mouse Click issueWPF FrameworkElement 鼠标点击问题
【发布时间】:2011-01-25 18:20:02
【问题描述】:

在 WPF 应用程序中,我在 Grid 中有一堆 CustomControl。为了处理鼠标点击它们,我使用 Grid 的 MouseLeftButtonDown 事件,并在事件处理程序中检查点击了哪个 CustomControl:

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement feSourceComm = e.Source as FrameworkElement;
        MyCustomControl SCurrentComm = new MyCustomControl();            
        try
        {
            SCurrentComm = (MyCustomControl)feSourceComm;
        }
        catch (Exception)
        {
...

当我将所有 CustomControls 放在 UserControl 中,然后放在 Grid 中时,问题就出现了。在这种情况下,方法不起作用。
我通过e.Source.GetType().ToString();检查了每种情况下的点击源类型,并得到以下结果:

没有问题的时候(如果我把CustomControls放在没有UserControl的Grid中)

MyProjectNamespace.MyCustomControl

当我将 CustomControls 放入 UserControl 然后放入 Grid

MyProjectNamespace.UserControls.MyUserControlName

当我将 CustomControls 放入 UserControl 然后放入 Grid 并通过 XamlReader.Load从外部文件加载 UserControl

System.Windows.Controls.UserControl

所以,我的问题是:
当 CustomControls 位于 UserControl 内部时,如何使它们显示为 e.Source

【问题讨论】:

    标签: c# wpf user-controls custom-controls frameworkelement


    【解决方案1】:

    e.OriginalSource 会告诉您点击发生在哪个特定元素上。如果那不是您的自定义控件,请向上走 OriginalSource 的父链,直到找到您的自定义控件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-13
      • 2010-12-10
      • 2010-12-01
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多