我想动态创建一个表单而不是XAML

时间:2011-10-17 14:14:06

标签: c# forms xaml dynamic

我想动态创建一个表单,而不是使用XAML来完成它。 表单驻留在选项卡控件中。以下是XAML代码:

这是一个XAML样本。

<TabControl>
<TabItem Header="SVRS Data" Name="tab_SVRS_Data2">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="113*" />
            <ColumnDefinition Width="113*" />
            <ColumnDefinition Width="55*" />
            <ColumnDefinition Width="58*" />
            <ColumnDefinition Width="113*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="13" />
            <RowDefinition Height="24" />
            <RowDefinition Height="15" />
            <RowDefinition Height="24" />
            <RowDefinition Height="24" />
            <RowDefinition Height="24" />
            <RowDefinition Height="24" />
            <RowDefinition Height="5" />
            <RowDefinition Height="24" />
            <RowDefinition Height="24" />
            <RowDefinition Height="5" />
            <RowDefinition Height="24" />
            <RowDefinition Height="24" />
            <RowDefinition Height="24" />
            <RowDefinition Height="24" />
            <RowDefinition Height="5" />
            <RowDefinition Height="24" />
            <RowDefinition Height="24" />
            <RowDefinition Height="24" />
            <RowDefinition Height="5" />
            <RowDefinition Height="24" />
            <RowDefinition Height="5" />
            <RowDefinition Height="24" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Button Name="btn_SaveSVRSDataDyn" Content="Save" Grid.Column="4" Grid.Row="1"  ToolTip="Saves the changes to the SVRS data " FontWeight="Bold" Click="btn_SaveSVRSData_Click" />

        <TextBox Name="SVRS_OuterEnvelopeId2" Grid.Column="0" Grid.Row="1" Style="{StaticResource InputBoxes}" Grid.ColumnSpan="2" />
        <Label Content="Outer Enveloppe ID" Grid.Column="0" Grid.Row="1" Style="{StaticResource LabelsOverlay}" Grid.ColumnSpan="2" Margin="5,0" />

        <TextBox Name="SVRS_LastName2" Grid.Row="3" Style="{StaticResource InputBoxes}" Grid.ColumnSpan="2" Uid="Elector"/>
        <Label Content="Last Name" Grid.Row="3" Style="{StaticResource LabelsOverlay}" Grid.ColumnSpan="2" Margin="5,0"/>

        <TextBox Name="SVRS_FirstName2" Grid.Row="4" Style="{StaticResource InputBoxes}" Grid.ColumnSpan="2" Uid="Elector"/>
        <Label Content="First Name" Grid.Row="4" Style="{StaticResource LabelsOverlay}" Grid.ColumnSpan="2" Margin="5,0"/>

        <TextBox Name="SVRS_MiddleName2" Grid.Column="2" Grid.Row="3" Style="{StaticResource InputBoxes}" Grid.ColumnSpan="3" Uid="Elector" Margin="1" />
        <Label Content="Middle Name" Grid.Column="2" Grid.Row="3" Style="{StaticResource LabelsOverlay}" Grid.ColumnSpan="3" Margin="5,0"/>

        <TextBox Name="SVRS_DaytimePhoneNumber2" Grid.Column="2" Grid.Row="4" Style="{StaticResource InputBoxes}" Grid.ColumnSpan="3" Uid="Elector" Margin="1" />
        <Label Content="Daytime Phone" Grid.Column="2" Grid.Row="4" Style="{StaticResource LabelsOverlay}" Grid.ColumnSpan="3" Margin="5,0"/>

        <ComboBox Name="SVRS_GenderTypeCode2" Grid.Row="5"  Style="{StaticResource ComboBoxes}" Uid="Elector"/>
        <Label Content="Gender" Grid.Row="5" Style="{StaticResource LabelsOverlay}"/>

        <TextBox Name="SVRS_DOB2" Grid.Column="1" Grid.Row="5"  Style="{StaticResource InputBoxes}" Uid="Elector" MaxLength="10" CharacterCasing="Upper" DataContext="{Binding}" LostFocus="SVRS_DOB_LostFocus" />
        <Label Content="DOB" Grid.Column="1" Grid.Row="5" Style="{StaticResource LabelsOverlay}"/>

        <TextBox Name="SVRS_EveningPhoneNumber2" Grid.Column="2" Grid.Row="5" Style="{StaticResource InputBoxes}" Grid.ColumnSpan="3" Uid="Elector" Margin="1" />
        <Label Content="Evening Phone" Grid.Column="2" Grid.Row="5" Style="{StaticResource LabelsOverlay}" Grid.ColumnSpan="3" Margin="5,0"/>

        <ComboBox Name="SVRS_Language2" Grid.Row="6"  Style="{StaticResource ComboBoxes}" Uid="Elector"/>
        <Label Content="Language" Grid.Row="6" Style="{StaticResource LabelsOverlay}"/>

        <TextBox Name="SVRS_EmailAddress2" Grid.Column="1" Grid.Row="6" Style="{StaticResource InputBoxes}" Grid.ColumnSpan="4" Uid="Elector" Margin="1" />
        <Label Content="email" Grid.Column="1" Grid.Row="6" Style="{StaticResource LabelsOverlay}" Grid.ColumnSpan="4" Margin="5,0"/>
    </Grid>
</TabItem>
</TabControl>

1 个答案:

答案 0 :(得分:1)

如果您希望在运行时通过代码生成这些控件,那么以下代码段将为您提供一个想法:

        // Define grid with columns and rows
        var grid = new Grid();
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(113, GridUnitType.Star) });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(55, GridUnitType.Star) });
        grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(13)});
        grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(24) });

        // Create each control and set its location
        var button = new Button { Name = "ButtonName", Content = "Button Content" };
        grid.Children.Add(button);
        Grid.SetColumn(button, 0);
        Grid.SetRow(button, 0);

        var textBox = new TextBox { Name= "TextBoxName"};
        grid.Children.Add(textBox);
        Grid.SetColumn(textBox, 1);
        Grid.SetRow(textBox, 0);

        // Create the tab control and add items
        var tabItem = new TabItem {Header = "Sample Header", Name = "TabName", Content = grid};

        var tabControl = new TabControl();
        tabControl.Items.Add(tabItem);

        MyWindow.AddChild(tabControl);