我想仅在宽度上扩展Wrap Panel,而Wrap Panel也不会在另一个已经存在的Stack Panel上绘制。问题可能与我添加到包装面板的按钮的标准配置有关,我还缺乏关于自动调整大小的知识,所以内容或本主题的指南将不胜感激。我添加到包裹面板的按钮应该添加文本框,但我的宽度是如何说的。
编辑:有没有办法在帖子/评论中添加视觉工作室项目?
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
stackPanel1.Width = Double.NaN;
stackPanel1.Height = Double.NaN;
stackPanel2.Width = Double.NaN;
stackPanel2.Height = Double.NaN;
AddWrapPanelMaster();
}
Button AddButtonSlave(Button but)
{
but.Click += new RoutedEventHandler(this.buttClickSlave);
return (but);
}
Button AddButtonMaster(Button but)
{
but.Click += new RoutedEventHandler(this.buttClickMaster);
return (but);
}
void buttClickMaster(Object sender, EventArgs e)
{
TextBox txtB1 = new TextBox();
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
Button s = (Button)sender;
WrapPanel wp = (WrapPanel)s.Parent;
wp.Children.Add(txtB1);
}
void buttClickSlave(Object sender, EventArgs e)
{
Button s = (Button)sender;
if (s.Background != Brushes.Blue && s.Background != Brushes.Red && s.Background != Brushes.Green)
s.Background = Brushes.Red;
else
if (s.Background == Brushes.Green)
s.Background = Brushes.Red;
else
if (s.Background == Brushes.Blue)
s.Background = Brushes.Green;
else
if (s.Background == Brushes.Red)
s.Background = Brushes.Blue;
}
void AddTextBox(Button sender) {
TextBox txtB1 = new TextBox();
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
WrapPanel s = (WrapPanel)sender.Parent;
s.Children.Add(txtB1);
// Add the buttons to the parent WrapPanel using the Children.Add method.
}
void AddWrapPanelSlave() {
WrapPanel myWrapPanel = new WrapPanel();
myWrapPanel.Background = System.Windows.Media.Brushes.Azure;
myWrapPanel.Orientation = Orientation.Horizontal;
myWrapPanel.Width = 200;
myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
myWrapPanel.VerticalAlignment = VerticalAlignment.Top;
// Define 3 button elements. The last three buttons are sized at width
// of 75, so the forth button wraps to the next line.
Button btn1 = new Button();
btn1 = AddButtonSlave(btn1);
btn1.Content = "Button 1";
btn1.Width = 75;
Button btn2 = new Button();
btn2.Content = "Button 2";
btn2.Width = 75;
// Add the buttons to the parent WrapPanel using the Children.Add method.
myWrapPanel.Children.Add(btn1);
myWrapPanel.Children.Add(btn2);
this.stackPanel1.Children.Add(myWrapPanel);
}
void AddWrapPanelMaster()
{
WrapPanel myWrapPanel = new WrapPanel();
myWrapPanel.Background = System.Windows.Media.Brushes.Azure;
myWrapPanel.Orientation = Orientation.Horizontal;
myWrapPanel.Width = 200;
myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
myWrapPanel.VerticalAlignment = VerticalAlignment.Top;
// Define 3 button elements. The last three buttons are sized at width
// of 75, so the forth button wraps to the next line.
TextBox txtB1 = new TextBox();
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
Button btn1 = new Button();
btn1 = AddButtonMaster(btn1);
btn1.Content = "Add";
btn1.Width = 75;
// Add the buttons to the parent WrapPanel using the Children.Add method.
myWrapPanel.Children.Add(txtB1);
myWrapPanel.Children.Add(btn1);
this.stackPanel2.Children.Add(myWrapPanel);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
AddWrapPanelSlave();
this.Show();
}
}
}