如何设置和获得控件相对于Canvas的位置
1个回答
展开全部
private void AddCanvasControl(List<string> aTypeStrList)
{
int aLocationX = 0;
int aLocationY = 0;
rCanvas.Children.Clear();
foreach (string aStrType in aTypeStrList)
{
Button aBtn = new Button();
aBtn.Content = aStrType;
aBtn.Height = 20;
Font f = new Font("SimSun", 7F, System.Drawing.FontStyle.Regular);
System.Drawing.Size aSize = System.Windows.Forms.TextRenderer.MeasureText(aStrType, f); //获得字符在控件中显示的尺寸
aBtn.Width = aSize.Width + 20;
aBtn.HorizontalAlignment = HorizontalAlignment.Left;
rCanvas.Children.Add(aBtn);
if (aLocationX + aBtn.Width > rCanvas.RenderSize.Width)
{
aLocationY += 25;
aLocationX = 0;
}
Canvas.SetLeft(aBtn, aLocationX); //设置按钮在容器中的位置
Canvas.SetTop(aBtn, aLocationY);
aLocationX += (int)aBtn.Width + 10;
}
}
{
int aLocationX = 0;
int aLocationY = 0;
rCanvas.Children.Clear();
foreach (string aStrType in aTypeStrList)
{
Button aBtn = new Button();
aBtn.Content = aStrType;
aBtn.Height = 20;
Font f = new Font("SimSun", 7F, System.Drawing.FontStyle.Regular);
System.Drawing.Size aSize = System.Windows.Forms.TextRenderer.MeasureText(aStrType, f); //获得字符在控件中显示的尺寸
aBtn.Width = aSize.Width + 20;
aBtn.HorizontalAlignment = HorizontalAlignment.Left;
rCanvas.Children.Add(aBtn);
if (aLocationX + aBtn.Width > rCanvas.RenderSize.Width)
{
aLocationY += 25;
aLocationX = 0;
}
Canvas.SetLeft(aBtn, aLocationX); //设置按钮在容器中的位置
Canvas.SetTop(aBtn, aLocationY);
aLocationX += (int)aBtn.Width + 10;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询