在WPF中-如何将buttonClick事件从一个用户控件触发到另一个用户控件? [英] In WPF - How to fire buttonClick event from one user-control to other user-control?

查看:571
本文介绍了在WPF中-如何将buttonClick事件从一个用户控件触发到另一个用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有2个单独的用户控件(每个控件都在其他文件中):

1. DataGrid

2.单独的用户控件,其中包含按钮.

我正在尝试找到一种方法,当我按下第二个用户控件上的按钮时,我可以捕获DataGrid用户控件上的事件并对该事件执行某些操作(例如,显示弹出窗口或像这样).

我试图查找引发事件",隧道",冒泡事件"等,但没有找到解决方法.

非常感谢帮助人员:)

Hi
I have 2 separate user controls (each one of them on other file):

1. DataGrid

2. Separate User-Control Which contains button.

I''m trying to find a way that when i''m pressing on a button on the 2nd user control, I can catch the event on the DataGrid user-control and to do something with that event (for example, show popup or something like that).

I tried to look for "Raised Event", "Tunneling", "bubbling events" etc. but didn''t find a way to do so.

Thanks a lot for the helpers :)

推荐答案

不要仅仅通过看"您提到的主题来尝试找到方法".在没有特定问题的情况下阅读它们,以了解事件的工作原理以及它们在WPF中的工作原理.现在,您是如此困惑,甚至无法正确表达自己想要的东西.

因为没有这样的事情,所以在...控件上捕获事件"的整个想法是错误的.偶数没有被捕获",甚至没有在任何特定控件上进行处理,但是它由某个特定类的某些代码触发.它可以由任何事件处理程序处理,您可以将其添加到任何事件实例的调用列表中.并且此事件处理程序与任何特定控件都不相关,并且可以是任何类的方法,也可以是(最好)在任何类的代码中添加的匿名方法.

下一件事:您无法触发Button.Click之类的事件.可以触发该事件,并且实际上已经将其编码为在类Button中触发.您无需解雇,实际上,您永远也不能解雇.只能在声明事件的类中触发事件;与常规委托实例相比,这是事件的重要的万无一失的局限性.

您可以对已定义的事件进行的全部操作就是将一些事件处理程序添加到其调用列表中,或者删除以前添加的处理程序:

Don''t try to "find a way" by just "looking" at the topics you have mentioned — first actually read them without connection to your particular problem to understand how events work and then how do they work in WPF. Right now you are so confused that you cannot even formulate what do you want correctly.

The whole idea of "catch the event on the… control…" is wrong because there is no such thing. The even is not "caught" and not even handled on any particular control, but it is fired by some code of some particular class. It is handled by any event handler which you can add to an invocation list of any event instance; and this event handler is not related to any particular control and can be a method of any class or (better) an anonymous method added in the code of any class.

Next thing: you cannot fire an event like Button.Click. This event can be fired and actually already coded to fire in the class Button. You do not need to fire it and, in fact, you never can. An event can only be fired in the class where it is declared; this is one of the important fool-proof limitations of events compared to regular delegate instances.

All you can do with an already defined event is adding some event handler to its invocation list or removing a previously added handler:

class AnyClassAnyAtAll {

    void AnyMethod() {
        myButton.Click += (sender, eventArgs) => {
            //you can do this, but for a button there is nothing specific to know about it:
            Button instance = (Button)sender; //will be myButton, but without using external variable
            //...
            //do something essential, for example:
            DoSomethingOnButtonClick();
        }
    }

    void DoSomethingOnButtonClick() {/*...*/}

}



如果仍然不清楚,则应执行以下操作:1)由于尚未准备好,将WPF应用程序搁置一会儿; 2)从头到尾阅读一些C#手册(不是WPF); 3)确保您可以解决手册中的所有或大部分简单练习,更喜欢控制台应用程序; 4)回到更复杂的主题,如WPF.

祝你好运,

-SA



If this is still not clear, you should do the following: 1) set your WPF application aside for a while, as you are not ready yet; 2) read some C# manual (not WPF) from the beginning to the end; 3) make sure you can solve all or most of the simple exercises from the manual, prefer console applications; 4) come back to more complex topics like WPF.

Good luck,

—SA


Here is how to fire an event on another 'thing'.  in this case its a usercontrol.

selectedCtrl.RaiseEvent(new RoutedEventArgs(UserControl.LoadedEvent, selectedCtrl));

I am firing the load event of an object of UserControl.  the specific object is selectCtrl.


这篇关于在WPF中-如何将buttonClick事件从一个用户控件触发到另一个用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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