WPF Stackpanel 的问题:如何对 Circle 中的元素进行分组? [英] Problem with WPF Stackpanel: how can I group elements in Circle?

查看:29
本文介绍了WPF Stackpanel 的问题:如何对 Circle 中的元素进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些圆形控件.我想将它们分组以添加一个常见的处理程序,如下所示 在 WPF 中,我可以像在 Javascript/Jquery 中一样将相同的点击处理程序一次附加到多个按钮吗?

I have some controls in circle. I want to group them to add a common handler as seen here In WPF can I attach the same click handler to multiple buttons at once like I can in Javascript/Jquery?

但是如果我使用 Stackpanel,它们就不会再绕圈子了,那么有什么替代 stackpanel 的方法呢?

But if I use Stackpanel they won't be no more in circle so what alternative to stackpanel ?

这在 WPF 中是不可能的吗?

So is this impossible in WPF ?

推荐答案

如果您绝对确定要对所有按钮使用相同的 eventhadler,您可以使用您提到的链接下描述的相同技巧.您可以使用任何种类的容器(网格、画布等)代替 StackPanel.

If you are absolutely sure, that you want to use the same eventhadler for all the buttons, you can use the same trick described under the link you mentioned. Instead of StackPanel you can use any variety of containers (Grid, Canvas etc).

另一种方法是,如果您使用画布(或网格),并将按钮放在圆形形状中(使用按钮上的 Canvas.Left/Right/Top/Bottom 属性或在网格的情况下)保证金财产).然后你可以为 Canvas 的 MouseLeftButtonDown 事件创建一个事件处理程序.通过这种方式,您可以捕获 Canvas 内的每次鼠标点击,但有时,您可能希望区分 Button 和 Canvas 中的其他所有内容(如果需要).

An alternative approach is if you use a Canvas (or a Grid), and put the Buttons in it in a circular shape (using the Canvas.Left/Right/Top/Bottom properties on the Buttons or in case of a Grid the Margin property). Then you could create an eventhandler for MouseLeftButtonDown event of the Canvas. This way you can catch every mouseclick inside the Canvas, but then sometimes, you would want to make a difference between Buttons and everything else in the Canvas (if you need).

这是我的替代方案的一小段代码(为了更好地理解):

Here's a little code for my alternative (for better understanding):

<Canvas MouseLeftButtonDown="Canvas_MouseLeftButtonDown">
    <Button Canvas.Left="10", Canvas.Top="10"/>
    <Button Canvas.Left="60", Canvas.Top="10"/>
</Canvas>


void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    //if you want to handle the buttonclicks only
    if(e.Source is Button){
    do_your_stuff();
    }
}

这篇关于WPF Stackpanel 的问题:如何对 Circle 中的元素进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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