代码之家  ›  专栏  ›  技术社区  ›  sublimeaces

我是xaml新手,对stackpanel和添加内容有疑问

  •  0
  • sublimeaces  · 技术社区  · 7 年前

    我在xaml上的代码如下所示

       </Grid>
        <Grid Grid.Column="1" Grid.Row="0" >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" ></RowDefinition>
                <RowDefinition Height="Auto" ></RowDefinition>
                <RowDefinition Height="*" ></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
    
            <Label Grid.Column="0" Grid.Row="0" HorizontalAlignment="Center" Content="Updates Avalable"></Label>
    
            <Button Name="btnUpdate" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center" MouseDoubleClick="btnUpdate_MouseDoubleClick" >Check For Available Updates</Button>
            <StackPanel Name="controlHolder" Grid.Column="0" Grid.Row="1">
    
            </StackPanel>
        </Grid>
    

    然后我创建一个自定义控件,并尝试将其添加到stackPanel,如下所示

                File_Type_Control_Green ftcg = ftc.GeneratefileTypeControl("file", "hello", "3mb", "someurl", "2-3-4");
            controlHolder.Children.Add(ftcg);
    

    然而,当我这样做时,它实际上会把它放在按钮内。

    在windows窗体中,您只需将控件添加到面板中就可以了。没问题,但这似乎是wpf中的一个大问题。我不知道该怎么解决这个问题。有人能给我指出正确的方向吗?

    主要目标是将新控件动态添加到堆栈面板。我还没有这样做,但我会添加xy位置,以适当地隔开控件,假设您可以像在面板中那样做。

    谢谢

    1 回复  |  直到 7 年前
        1
  •  2
  •   KMC    7 年前

    您让Button和StackPanel填充相同的网格单元(grid.Column=“0”grid.Row=“1”)。StackPanel具有更高的Z索引,但默认情况下它具有透明的背景,当它们重叠时,您可能无法从视觉上区分它们。也许您可以将按钮和StackPanel包装到另一个StackPanel中。

    <Grid Grid.Column="1" Grid.Row="0" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" ></RowDefinition>
            <RowDefinition Height="Auto" ></RowDefinition>
            <RowDefinition Height="*" ></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Label Grid.Column="0" Grid.Row="0" HorizontalAlignment="Center" Content="Updates Avalable"></Label>
        <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Vertical">
            <Button Name="btnUpdate" />
            <StackPanel Name="controlHolder" />
        </StackPanel>
    </Grid>