开发者

Problem with WPF Stackpanel: how can I group elements in Circle?

I have 开发者_StackOverflow社区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?

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

So is this impossible in WPF ?


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).

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();
    }
}


Would the PathListBox from Blend be an option? You can read about it here. It is described for Silverlight but Blend also has a WPF version.

It would help you to arrange the controls any way you like and still keep them contained in a single container to handle events.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜