开发者

I'd like to create a form dynamically instead of XAML

开发者 https://www.devze.com 2023-04-13 08:47 出处:网络
I would like to create a form dynamically instead of using the XAML to do it. The form reside into a tab control. Below is XAML code:

I would like to create a form dynamically instead of using the XAML to do it. The form reside into a tab control. Below is XAML code:

this is a XAML sample.

<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"/>
    开发者_JS百科    <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>


If you mean that you want to generate these controls by code at runtime, then the following snippet will give you an idea:

        // 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);
0

精彩评论

暂无评论...
验证码 换一张
取 消