【问题标题】:I am new to xaml and have questions about stackpanel and adding things to them我是 xaml 的新手,对 stackpanel 和向其中添加内容有疑问
【发布时间】:2018-03-25 23:42:00
【问题描述】:

我在 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 位置以正确分隔控件,假设您可以像在面板中那样做。

谢谢!

【问题讨论】:

  • 您让 Button 和 StackPanel 填充了相同的网格列和行,因此您无法从视觉上区分它们,即使 UserControl 是 StackPanel 的子项。将它们放在不同的网格单元格中或在堆栈面板上设置背景颜色,以便您可以看到 UserControl 的位置。
  • .......................所以这是那些日子之一,是吗?

标签: c# xaml custom-controls stackpanel


【解决方案1】:

您让 Button 和 StackPanel 都填充了同一个网格单元格 (Grid.Column="0" Grid.Row="1")。 StackPanel 具有更高的 Z-Index,但默认情况下它具有透明背景,并且由于它们重叠,您可能无法从视觉上区分它们。也许您可以将 Button 和 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>

【讨论】:

  • 谢谢。这是我再次忽略显而易见的日子之一 lmao
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-09
  • 2011-08-31
相关资源
最近更新 更多