2

I'm creating a silverlight application where I have to dynamically create buttons. But I need to place them in a circle around the button that I click to generate the other buttons (picture here, the buttons should go on the black line surrounding the 'test project' button)

I don't know how many buttons will be generated each time but I do know the size of each button is static. I'm not quite sure how to do this. Currently my button creation is as follows

                foreach (Item a in itemList)
                {
                    Button newButton = new Button();
                    newButton.Height = 50;
                    newButton.Width = 50;
                    newButton.Content = a.getName();
                    newButton.Click += new RoutedEventHandler(addedClick);
                    newButton.HorizontalAlignment = HorizontalAlignment.Left;
                    newButton.VerticalAlignment = VerticalAlignment.Top;
                    newButton.Margin = new Thickness(0, 0, 0, 0);
                    newButton.Style = (Style)Application.Current.Resources["RB"];
                    buttons.Add(newButton);
                }

My biggest problem is that I'm not quite sure how to get the center point of the 'test project' button.

EDIT: Okay, now that I have a set of coordinates for each button, how exactly do I go about placing them? I'm not sure how to use a canvas. I tried to set one up but it keeps acting like a stackpanel (no .setLeft/.setTop).

4

3 回答 3

3

你的意思是类似于圆方程:

Double distanceFromCenter = 5;
Double angleInDegrees = 90;
Double angleAsRadians = (angleInDegrees* Math.PI) / 180.0;
Double centerX = 100;
Double centerY = 100;

Double x = centerX +  Math.Cos(angleAsRadians) * distanceFromCenter;
Double y = centerY + Math.Sin(angleAsRadians) * distanceFromCenter;

这应该给你一个点,即90 度的distanceFromCenter单位远离。请注意,这仅适用于弧度,因此我们必须转换为弧度。(centerX, center)angle

于 2013-05-29T13:19:52.307 回答
2
var radius = 100;
var angle = 360 / itmeList.Count * Math.PI / 180.0f;
var center = new Point(100, 100);

for (int i = 0; i < itemList.Count; i++)
            {
                var x = center.X +  Math.Cos(angle * i) * radius;
                var y = center.Y +  Math.Sin(angle * i) * radius;
                Button newButton = new Button();
                newButton.RenderTransformOrigin = new Point(x, y);
                newButton.Height = 50;
                newButton.Width = 50;
                newButton.Content = a.getName();
                newButton.Click += new RoutedEventHandler(addedClick);
                newButton.HorizontalAlignment = HorizontalAlignment.Left;
                newButton.VerticalAlignment = VerticalAlignment.Top;
                newButton.Margin = new Thickness(0, 0, 0, 0);
                newButton.Style = (Style)Application.Current.Resources["RB"];
                buttons.Add(newButton);
            }
于 2013-05-29T13:23:21.513 回答
1

假设您希望您的按钮在圆圈上均匀分布,您应该首先生成您希望它们处于的角度列表。例如

double eachSection = 2 * Math.PI / count;
var anglesInRadians = Enumerable.Range(0, count).Select(x => x * eachSection);

然后使用这个公式找到每个角度的 x/y 坐标,并使用 aCanvas或其他东西将按钮定位在那些位置

public static Point PointOnCircle(double radius, double angleInRadians, Point origin)
{
    double x = radius * Math.Cos(angleInRadians) + origin.X;
    double y = radius * Math.Sin(angleInRadians) + origin.Y;

    return new Point(x, y);
}
于 2013-05-29T13:25:25.747 回答