繁体   English   中英

在包装面板中添加包装面板

[英]Adding Wrap Panel in to a Stack Panel

为什么这没有用? 堆栈面板具有默认设置按钮不应超出边界。

void AddWrapPanel() {

    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.Content = "Button 1";
    btn1.Width = 200;
    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);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    AddWrapPanel();
}

这里是XAML

  <Window x:Class="AmpelThingy.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="349,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
        <StackPanel Height="214" HorizontalAlignment="Left" Margin="32,33,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="392" />
    </Grid>
</Window>

这是一个简单的问题,我不知道我还能写些什么细节

在这里,您创建了一个方法“ button1_Click”,但是WPF如何知道何时应实际调用它? 因此,您必须将按钮click事件挂接到“ button1_Click”方法。 您可以使用Click =“ button1_Click”属性来完成此操作。 现在,您的XAML如下所示:

        <Grid>
            <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="349,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
            <StackPanel Height="214" HorizontalAlignment="Left" Margin="32,33,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="392" />
        </Grid>

希望这个能对您有所帮助..!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM