WPF如何获得一个用户控件继承一个按钮? [英] WPF How to get a UserControl to inherit a Button?
问题描述
我创建了由一对夫妇椭圆和标签的用户控件。我说这只是罚款,另一种形式的东西都找pretty时髦。
I created a UserControl which consists of a couple ellipses and labels. I added it just fine to another form and things were looking pretty snazzy.
然后我就开始添加一些事件处理程序,发现我做对照没有暴露一个Click事件。哎呀。很容易解决,对不对?刚回到code-背后我做的用户控件,并让它继承按钮。
Then I went to start adding some event handlers and discovered that the control I made did not expose a Click event. Oops. Easy to fix, right? Just go back to the code-behind on the UserControl I made and let it inherit Button.
不幸的是,这样做造成了显示myControl分部声明一定不能指定不同的基类的消息。这令人惊讶,因为我没有宣布任何其他基类。搜索有问题的部分返回任何结果无论是。
Unfortunately, doing this caused a "Partial declarations of MyControl must not specify different base classes" message. This came as a surprise since I haven't declared any other base classes. Searching for the Partial in question returned no results either.
有没有办法战胜这个问题?如果没有,你会在这里是最简单的解决方法吗?我的目标很简单,就是让在用户控件Click事件。
Is there a way to defeat this issue? If not, what would be the easiest workaround here? My goal is simply to get a Click event on the UserControl.
推荐答案
是的,你声明其他基类:)只要信任编译器:)
Yes, you've declared other base classes :) Just trust compiler :)
当你写<用户控件...>< /用户控件>
在XAML中,你继承用户控件。如果你想继承一个按钮,使用<按钮...>< /按钮>
而不是和:在code隐藏文件按钮。
When you writing <UserControl ...></UserControl>
in XAML, you are subclassing UserControl. If you want to subclass a Button, use <Button ...></Button>
instead and ": Button" in code-behind file.
但我强烈建议你不要继承按钮,它给你的任务矫枉过正。您可以使用的MouseLeftButtonUp事件而不是Click事件。
But I'm strongly encourage you to not subclass Button, its an overkill for you task. You can use MouseLeftButtonUp event instead of the Click event.
这篇关于WPF如何获得一个用户控件继承一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!