一天未解决问题,请帮下我,没有办法,今天要加班了

app专项测试 2014-10-21 04:53:23
我这在一个Canvas里面,加载有很多的控件,例如按钮等;现在我右键出来一个菜单MENU菜单,菜单的内容是:菜单1、菜单2等;单击“菜单1”进入这个菜单的事件处理,我怎么判断在Canvas里面单击的具体按钮ID呢,很郁闷,不知道怎么写?
UIElement uid = new UIElement();

uid=ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem));
这个只能获取Canvas,获取不到单击了那个按钮,求大神相助;
...全文
498 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
app专项测试 2014-10-22
  • 打赏
  • 举报
回复
引用 26 楼 juckxu 的回复:
private UIElement CurUI; private void RegButton(Canvas can) { foreach (UIElement ctl in can.Children) { ctl.MouseDown += ctl_MouseDown; } } void ctl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { CurUI=sender as UIElement ; } RegButton(Canvas can)这句放在你的window的load事件中,而不是菜单的单击事件中。你的菜单单击事件mi_click直接访问CurUI就是触发菜单的控件
你的这个方法我解决问题了,真心感谢你,认识你很开心,真的谢谢了,结贴,分数你的最高了,哈哈。
於黾 2014-10-22
  • 打赏
  • 举报
回复
同25楼,楼主先弄明白,你的菜单用什么代码弹出来的,弹出的时候是否知道按下的是什么按钮 或者根本不需要点在按钮上,右键也可以弹出菜单??那你的菜单就跟按钮根本没有任何关系
juckxu 2014-10-22
  • 打赏
  • 举报
回复
private UIElement CurUI; private void RegButton(Canvas can) { foreach (UIElement ctl in can.Children) { ctl.MouseDown += ctl_MouseDown; } } void ctl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { CurUI=sender as UIElement ; } RegButton(Canvas can)这句放在你的window的load事件中,而不是菜单的单击事件中。你的菜单单击事件mi_click直接访问CurUI就是触发菜单的控件
  • 打赏
  • 举报
回复
楼主你得先确定事件是需要谁来发起? 到底是按钮还是canvas. 如果是按钮,那就先触发按钮的事件,再在这个事件里面去继续主动触发canvas 你现在明明是canvas发起的,却又要定位到具体的button,就是莫名其妙. 有多少按钮,绑多少次事件,
本王到此一游 2014-10-21
  • 打赏
  • 举报
回复
顺便学习了。谢谢
app专项测试 2014-10-21
  • 打赏
  • 举报
回复
引用 20 楼 juckxu 的回复:
你的mi_Click是菜单点击处理的?如果是foreach的整个语句要放在窗体的load事件中了
是的,是菜单单击事件的处理事件; 放在LOAD中?
app专项测试 2014-10-21
  • 打赏
  • 举报
回复
引用 19 楼 juckxu 的回复:
将UIElement改成FrameworkElement,
还是NULL
juckxu 2014-10-21
  • 打赏
  • 举报
回复
你的mi_Click是菜单点击处理的?如果是foreach的整个语句要放在窗体的load事件中了
juckxu 2014-10-21
  • 打赏
  • 举报
回复
将UIElement改成FrameworkElement,
app专项测试 2014-10-21
  • 打赏
  • 举报
回复
引用 17 楼 juckxu 的回复:
[quote=引用 16 楼 juckxu 的回复:] [quote=引用 15 楼 fanxiaojuan 的回复:] [quote=引用 11 楼 duanzi_peng 的回复:] [quote=引用 6 楼 fanxiaojuan 的回复:] [quote=引用 4 楼 duanzi_peng 的回复:] 你是在 按钮上的右键??
恩,先单击按钮,然后右键单击出来 MENU菜单,然后进入MENU菜单的处理事件,怎么判断我是单击的那个按钮,这个按钮是CANVAS里面的按钮。[/quote] 参见:http://blog.csdn.net/duanzi_peng/article/details/17094639 获取父元素。[/quote] 我说Canvas里面有很多控件类型,比如还有很多checkbox,botton,textbox等,我在任何一个控件上右键单击出来一个MENU菜单,例如MENU菜单里面有“功能1”,“功能2”,这时候我但是功能1,进入了这个MENU的处理事件,我要获取我是在Canvas里面的那个控件上单击出来MENU菜单的,其实就是这样的需求。[/quote]

private UIElement CurUI;
 private void RegButton(Canvas can)
        {
            foreach (UIElement ctl in can.Children)
            {
                    ctl.MouseDown += ctl_MouseDown;
            }
        }
 
        void ctl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            CurUI=sender as UIElement ;
        }
     
[/quote] 这个应该可以吧,[/quote] 获取的CurUI为空null
juckxu 2014-10-21
  • 打赏
  • 举报
回复
引用 16 楼 juckxu 的回复:
[quote=引用 15 楼 fanxiaojuan 的回复:] [quote=引用 11 楼 duanzi_peng 的回复:] [quote=引用 6 楼 fanxiaojuan 的回复:] [quote=引用 4 楼 duanzi_peng 的回复:] 你是在 按钮上的右键??
恩,先单击按钮,然后右键单击出来 MENU菜单,然后进入MENU菜单的处理事件,怎么判断我是单击的那个按钮,这个按钮是CANVAS里面的按钮。[/quote] 参见:http://blog.csdn.net/duanzi_peng/article/details/17094639 获取父元素。[/quote] 我说Canvas里面有很多控件类型,比如还有很多checkbox,botton,textbox等,我在任何一个控件上右键单击出来一个MENU菜单,例如MENU菜单里面有“功能1”,“功能2”,这时候我但是功能1,进入了这个MENU的处理事件,我要获取我是在Canvas里面的那个控件上单击出来MENU菜单的,其实就是这样的需求。[/quote]

private UIElement CurUI;
 private void RegButton(Canvas can)
        {
            foreach (UIElement ctl in can.Children)
            {
                    ctl.MouseDown += ctl_MouseDown;
            }
        }
 
        void ctl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            CurUI=sender as UIElement ;
        }
     
[/quote] 这个应该可以吧,
juckxu 2014-10-21
  • 打赏
  • 举报
回复
引用 15 楼 fanxiaojuan 的回复:
[quote=引用 11 楼 duanzi_peng 的回复:] [quote=引用 6 楼 fanxiaojuan 的回复:] [quote=引用 4 楼 duanzi_peng 的回复:] 你是在 按钮上的右键??
恩,先单击按钮,然后右键单击出来 MENU菜单,然后进入MENU菜单的处理事件,怎么判断我是单击的那个按钮,这个按钮是CANVAS里面的按钮。[/quote] 参见:http://blog.csdn.net/duanzi_peng/article/details/17094639 获取父元素。[/quote] 我说Canvas里面有很多控件类型,比如还有很多checkbox,botton,textbox等,我在任何一个控件上右键单击出来一个MENU菜单,例如MENU菜单里面有“功能1”,“功能2”,这时候我但是功能1,进入了这个MENU的处理事件,我要获取我是在Canvas里面的那个控件上单击出来MENU菜单的,其实就是这样的需求。[/quote]

private UIElement CurUI;
 private void RegButton(Canvas can)
        {
            foreach (UIElement ctl in can.Children)
            {
                if (ctl is Button)
                {
                    ctl.MouseDown += ctl_MouseDown;
                }
            }
        }
 
        void ctl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            CurUI=sender as UIElement ;
        }
     
app专项测试 2014-10-21
  • 打赏
  • 举报
回复
引用 11 楼 duanzi_peng 的回复:
[quote=引用 6 楼 fanxiaojuan 的回复:] [quote=引用 4 楼 duanzi_peng 的回复:] 你是在 按钮上的右键??
恩,先单击按钮,然后右键单击出来 MENU菜单,然后进入MENU菜单的处理事件,怎么判断我是单击的那个按钮,这个按钮是CANVAS里面的按钮。[/quote] 参见:http://blog.csdn.net/duanzi_peng/article/details/17094639 获取父元素。[/quote] 我说Canvas里面有很多控件类型,比如还有很多checkbox,botton,textbox等,我在任何一个控件上右键单击出来一个MENU菜单,例如MENU菜单里面有“功能1”,“功能2”,这时候我但是功能1,进入了这个MENU的处理事件,我要获取我是在Canvas里面的那个控件上单击出来MENU菜单的,其实就是这样的需求。
app专项测试 2014-10-21
  • 打赏
  • 举报
回复
引用 12 楼 QQ234788028 的回复:
[quote=引用 3 楼 fanxiaojuan 的回复:] [quote=引用 1 楼 QQ234788028 的回复:] 你右键菜单是Canvas的对吧,你现在是要判断,在那个控件(按钮上右键的?)
Canvas里面有很多按钮控件,我任意单击一个按钮,然后右键出来MENU菜单,在这个MENU菜单里怎么知道我单击了那个按钮? 右键菜单是MENU菜单,现在判断的单击了那个按钮?[/quote]右键菜单是绑哪里?控件还是Canvas 没说清楚啊[/quote] 我说Canvas里面有很多控件类型,比如还有很多checkbox,botton,textbox等,我在任何一个控件上右键单击出来一个MENU菜单,例如MENU菜单里面有“功能1”,“功能2”,这时候我但是功能1,进入了这个MENU的处理事件,我要获取我是在Canvas里面的那个控件上单击出来MENU菜单的,其实就是这样的需求。
app专项测试 2014-10-21
  • 打赏
  • 举报
回复
引用 9 楼 juckxu 的回复:

private Button SelButton;
 private void RegButton()
        {
            Canvas can = new Canvas();
            foreach (UIElement ctl in can.Children)
            {
                if (ctl is Button)
                {
                    ctl.MouseDown += ctl_MouseDown;
                }
            }
        }

        void ctl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            SelButton=sender as Button;
        }
然后在你的菜单事件中访问selbutton就可以了啊
这个似乎不行吧,我说Canvas里面有很多控件类型,比如还有很多checkbox,botton,textbox等,我在任何一个控件上右键单击出来一个MENU菜单,例如MENU菜单里面有“功能1”,“功能2”,这时候我但是功能1,进入了这个MENU的处理事件,我要获取我是在Canvas里面的那个控件上单击出来MENU菜单的,其实就是这样的需求。 你这个是针对按钮,然后针对按钮的话,还是循环了所有按钮吧。
QQ234788028 2014-10-21
  • 打赏
  • 举报
回复
引用 3 楼 fanxiaojuan 的回复:
[quote=引用 1 楼 QQ234788028 的回复:] 你右键菜单是Canvas的对吧,你现在是要判断,在那个控件(按钮上右键的?)
Canvas里面有很多按钮控件,我任意单击一个按钮,然后右键出来MENU菜单,在这个MENU菜单里怎么知道我单击了那个按钮? 右键菜单是MENU菜单,现在判断的单击了那个按钮?[/quote]右键菜单是绑哪里?控件还是Canvas 没说清楚啊
exception92 2014-10-21
  • 打赏
  • 举报
回复
引用 6 楼 fanxiaojuan 的回复:
[quote=引用 4 楼 duanzi_peng 的回复:] 你是在 按钮上的右键??
恩,先单击按钮,然后右键单击出来 MENU菜单,然后进入MENU菜单的处理事件,怎么判断我是单击的那个按钮,这个按钮是CANVAS里面的按钮。[/quote] 参见:http://blog.csdn.net/duanzi_peng/article/details/17094639 获取父元素。
juckxu 2014-10-21
  • 打赏
  • 举报
回复
private Button SelButton; private void RegButton(Canvas can) { foreach (UIElement ctl in can.Children) { if (ctl is Button) { ctl.MouseDown += ctl_MouseDown; } } } void ctl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { SelButton=sender as Button; }
juckxu 2014-10-21
  • 打赏
  • 举报
回复

private Button SelButton;
 private void RegButton()
        {
            Canvas can = new Canvas();
            foreach (UIElement ctl in can.Children)
            {
                if (ctl is Button)
                {
                    ctl.MouseDown += ctl_MouseDown;
                }
            }
        }

        void ctl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            SelButton=sender as Button;
        }
然后在你的菜单事件中访问selbutton就可以了啊
app专项测试 2014-10-21
  • 打赏
  • 举报
回复
引用 7 楼 ranshouxu001 的回复:
你单击的按钮没有焦点吗。。。
请亲说出具体的想法,我已经没有思路了。
加载更多回复(7)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧