C# wpf 放在StackPanel中的确定位置

2024-11-25 17:18:53
推荐回答(1个)
回答1:

Button btn1 = new Button() { Name = "btn1", Content = "btn1", Width = 150, Height = 150 };
Button btn2 = new Button() { Name = "btn2", Content = "btn2", Width = 150, Height = 150 }; this.RegisterName("btn1", btn1);
this.RegisterName("btn2", btn2); stackPanel1.Children.Add(btn1);
stackPanel1.Children.Add(btn2);
Button _btn1 = (Button)stackPanel1.FindName("btn1");
Button _btn2 = (Button)stackPanel1.FindName("btn2");
int cc =0;
if (cc==0)
{
_btn1.HorizontalAlignment = HorizontalAlignment.Left;
_btn2.HorizontalAlignment = HorizontalAlignment.Right;
}
else
{
_btn1.HorizontalAlignment = HorizontalAlignment.Right;
_btn2.HorizontalAlignment = HorizontalAlignment.Left;
}