簡體   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