如何在C#中实例化整个XAML网格对象?

时间:2018-12-06 20:53:03

标签: c# wpf xaml object grid

我想在C#中实例化整个XAML Grid对象。我的网格对象是一个野兽,它具有9个用户输入字段,边框,标签和图像。

我还希望有Grid对象的五个实例,我想在运行时动态地实例化它们(按下按钮)。这可能吗?最好的方法是什么?这是我的Grid对象...

 <Grid Margin="2">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <Border Grid.Row="1" Grid.Column="0" Grid.RowSpan="6" Grid.ColumnSpan="8" BorderBrush="Black" BorderThickness="1" CornerRadius="5">
                            <Label Grid.Row="1" Grid.RowSpan="3" Grid.Column="0" Grid.ColumnSpan="8" Background="#505050" ></Label>
                        </Border>

                        <TextBlock Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="4" Text="TRANSFORM" TextAlignment="Center" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen"></TextBlock>
                        <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="8" Text="Position" VerticalAlignment="Bottom" TextAlignment="Left" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen" Margin="2"></TextBlock>
                        <TextBlock Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="1" Text="x " VerticalAlignment="Bottom" TextAlignment="Right" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen"></TextBlock>
                        <TextBlock Grid.Row="1" Grid.Column="4" Grid.ColumnSpan="1" Text="y " VerticalAlignment="Bottom" TextAlignment="Right" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen"></TextBlock>
                        <TextBlock Grid.Row="1" Grid.Column="6" Grid.ColumnSpan="1" Text="z " VerticalAlignment="Bottom" TextAlignment="Right" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen"></TextBlock>
                        <TextBox x:Name ="transform_x" Grid.Row="1" Grid.Column="3" Grid.ColumnSpan="1" TextAlignment="Center" VerticalAlignment="Bottom" FontSize="10" Background="#707070" Foreground="Black" FontFamily="./resources/#Pilsen" Margin="2" ></TextBox>
                        <TextBox x:Name ="transform_y" Grid.Row="1" Grid.Column="5" Grid.ColumnSpan="1" TextAlignment="Center" VerticalAlignment="Bottom" FontSize="10" Background="#707070" Foreground="Black" FontFamily="./resources/#Pilsen" Margin="2" ></TextBox>
                        <TextBox x:Name ="transform_z" Grid.Row="1" Grid.Column="7" Grid.ColumnSpan="1" TextAlignment="Center" VerticalAlignment="Bottom" FontSize="10" Background="#707070" Foreground="Black" FontFamily="./resources/#Pilsen" Margin="2" ></TextBox>

                        <TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="8" Text="Rotation"  VerticalAlignment="Bottom"  TextAlignment="Left" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen" Margin="2"></TextBlock>
                        <TextBlock Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="1" Text="x " VerticalAlignment="Bottom" TextAlignment="Right" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen"></TextBlock>
                        <TextBlock Grid.Row="2" Grid.Column="4" Grid.ColumnSpan="1" Text="y " VerticalAlignment="Bottom" TextAlignment="Right" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen"></TextBlock>
                        <TextBlock Grid.Row="2" Grid.Column="6" Grid.ColumnSpan="1" Text="z " VerticalAlignment="Bottom" TextAlignment="Right" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen"></TextBlock>
                        <TextBox x:Name ="rotation_x" Grid.Row="2" Grid.Column="3" Grid.ColumnSpan="1" TextAlignment="Center" VerticalAlignment="Bottom" FontSize="10" Background="#707070" Foreground="Black" FontFamily="./resources/#Pilsen" Margin="2"></TextBox>
                        <TextBox x:Name ="rotation_y" Grid.Row="2" Grid.Column="5" Grid.ColumnSpan="1" TextAlignment="Center" VerticalAlignment="Bottom" FontSize="10" Background="#707070" Foreground="Black" FontFamily="./resources/#Pilsen" Margin="2"></TextBox>
                        <TextBox x:Name ="rotation_z" Grid.Row="2" Grid.Column="7" Grid.ColumnSpan="1" TextAlignment="Center" VerticalAlignment="Bottom" FontSize="10" Background="#707070" Foreground="Black" FontFamily="./resources/#Pilsen" Margin="2"></TextBox>

                        <TextBlock Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="8" Text="Scale" VerticalAlignment="Bottom" TextAlignment="Left" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen" Margin="2"></TextBlock>
                        <TextBlock Grid.Row="3" Grid.Column="2" Grid.ColumnSpan="1" Text="x " VerticalAlignment="Bottom" TextAlignment="Right" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen"></TextBlock>
                        <TextBlock Grid.Row="3" Grid.Column="4" Grid.ColumnSpan="1" Text="y " VerticalAlignment="Bottom" TextAlignment="Right" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen"></TextBlock>
                        <TextBlock Grid.Row="3" Grid.Column="6" Grid.ColumnSpan="1" Text="z " VerticalAlignment="Bottom" TextAlignment="Right" FontSize="10" Foreground="#FD3777" FontFamily="./resources/#Pilsen"></TextBlock>
                        <TextBox x:Name ="scale_x" Grid.Row="3" Grid.Column="3" Grid.ColumnSpan="1" TextAlignment="Center" VerticalAlignment="Bottom" FontSize="10" Background="#707070" Foreground="Black" FontFamily="./resources/#Pilsen" Margin="2"></TextBox>
                        <TextBox x:Name ="scale_y" Grid.Row="3" Grid.Column="5" Grid.ColumnSpan="1" TextAlignment="Center" VerticalAlignment="Bottom" FontSize="10" Background="#707070" Foreground="Black" FontFamily="./resources/#Pilsen" Margin="2"></TextBox>
                        <TextBox x:Name ="scale_z" Grid.Row="3" Grid.Column="7" Grid.ColumnSpan="1" TextAlignment="Center" VerticalAlignment="Bottom" FontSize="10" Background="#707070" Foreground="Black" FontFamily="./resources/#Pilsen" Margin="2"></TextBox>

                    </Grid>

2 个答案:

答案 0 :(得分:0)

用代码简单地新建一个网格

Grid grid = new Grid() { Margin = new Thickness(2) };

并向其中添加元素。

grid.Children.Add(new Border());
TextBlock t = new TextBlock();
t.SetValue(Grid.RowProperty, 0);
t.SetValue(Grid.ColumnProperty, 2);
grid.Children.Add(t);
//etc...

或者您可以将UserControl项目添加到项目中,然后将Grid放入其xaml中。然后,您可以根据需要更新尽可能多的MyGridControl。

答案 1 :(得分:0)

您可以执行以下操作:

  • 创建一个包含网格的UserControl,例如MyBeastGrid
  • 在目标表单中有一个占位符,应在其中放置那些实例化的网格,例如MyPlaceholder
  • 添加按钮(或应将{{​​1}}添加到MyBeastGrid的其他触发机制)
  • 在按钮的MyPlaceholder事件处理程序中,您只需调用Click

如果您需要将一些参数传递给MyPlaceholder.Children.Add(new MyBeastGrid());,请在其中定义一些公共属性,然后提供参数化的构造函数或使用MyBeastGrid调用网格

我建议您使用new MyBeastGrid() { Property1 = value1, Property2 = value2 };存储ScrollViewer实例,因为它可以滚动显示。