GroupBox扩展属性

时间:2012-12-31 02:23:54

标签: c# wpf wpf-controls

  • 在我的C#WPF窗口应用程序中,我使用了Group Box,里面有一些标签和文本框来插入一些数据
  • 我在该组框中的一个文本框旁边创建了一个名为(添加)的按钮
  • 我想按下(添加)按钮时:
    • 组框扩大并
    • 新文本框显示在第一个下方,以插入更多数据
    • (添加)按钮出现在新文本框旁边
  • 我希望你能帮助我..谢谢

enter image description here

1 个答案:

答案 0 :(得分:2)

看看这是否是您想要的,我使用了StackPanel'sDockPanel,这会随着内容的添加而增加,我还添加了5个额外TextBox's和{{1}具有折叠的可见性,这意味着它们在不可见时不会占用任何空间。我将它们添加到List中,然后在您单击按钮时检查可见性,使当时可以看到相应的控件。

<强> MainWindow.xaml

TextBlock's

<强> MainWindow.xaml.cs

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="214" Width="525" SizeToContent="WidthAndHeight">
    <Grid>
        <GroupBox Header="General Information"   HorizontalAlignment="Left"  Name="groupBox1" VerticalAlignment="Top">
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="4*"/>
                    <ColumnDefinition Width="3*"/>
                </Grid.ColumnDefinitions>
                <DockPanel Grid.Column="0" HorizontalAlignment="Left" >
                   <Button VerticalAlignment="Bottom"  Height="25"   Click="Button_Click" >Add</Button>
                </DockPanel>
                <StackPanel Name="stackpanelTB" Grid.Column="1"  Orientation="Vertical"  >
                    <StackPanel.Resources>
                        <Style TargetType="{x:Type TextBox}">
                            <Setter Property="Margin" Value="0,4,0,0"/>
                        </Style>
                    </StackPanel.Resources>
                    <TextBox  Name="textBox1" Width="240" Height="25"></TextBox>
                    <TextBox Name="textBox2" Width="240" Height="25"></TextBox>
                    <TextBox Name="textBox3" Width="240" Height="25"></TextBox>
                    <TextBox Name="textBox4" Width="240" Height="25"></TextBox>
                    <TextBox Name="textBox5" Width="240" Visibility="Collapsed" Height="25"></TextBox>
                    <TextBox Name="textBox6" Width="240" Visibility="Collapsed" Height="25"></TextBox>
                    <TextBox Name="textBox7" Width="240" Visibility="Collapsed" Height="25"></TextBox>
                    <TextBox Name="textBox8" Width="240" Visibility="Collapsed" Height="25"></TextBox>
                    <TextBox Name="textBox9" Width="240" Visibility="Collapsed" Height="25"></TextBox>
                </StackPanel>
                <StackPanel Name ="stackpanelCaption" Grid.Column="2" Orientation="Vertical">
                    <StackPanel.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="Margin" Value="0,4,0,0"/>
                        </Style>
                    </StackPanel.Resources>
                    <TextBlock Name="textBlock1" HorizontalAlignment="Right" Height="25">Ingredient ID</TextBlock>
                    <TextBlock Name="textBlock2" HorizontalAlignment="Right" Height="25">Ingredient Name</TextBlock>
                    <TextBlock Name="textBlock3" HorizontalAlignment="Right" Height="25">Chemical Str</TextBlock>
                    <TextBlock Name="textBlock4" HorizontalAlignment="Right" Height="25">Ingredient Salts</TextBlock>
                    <TextBlock Name="textBlock5" HorizontalAlignment="Right" Visibility="Collapsed" Height="25"></TextBlock>
                    <TextBlock Name="textBlock6" HorizontalAlignment="Right" Visibility="Collapsed" Height="25"></TextBlock>
                    <TextBlock Name="textBlock7" HorizontalAlignment="Right" Visibility="Collapsed" Height="25"></TextBlock>
                    <TextBlock Name="textBlock8" HorizontalAlignment="Right" Visibility="Collapsed" Height="25"></TextBlock>
                    <TextBlock Name="textBlock9" HorizontalAlignment="Right" Visibility="Collapsed" Height="25"></TextBlock>
                </StackPanel>
            </Grid>
       </GroupBox>
    </Grid>
</Window>