1

我有一个需要放入边框的网格,通过 XAML 执行此操作很容易,但如何通过 C# 执行此操作?到目前为止我发现的所有东西都想在每个单元格周围添加边框。

我需要它以与 XAML 相同的方式出现,请帮助!

我无法在此处正确发布 XAML:

<Border Grid.Column="1" 
        Grid.Row="0" 
        Background="AliceBlue" 
        BorderBrush="Black" 
        BorderThickness="4" 
        x:Name="Side6" 
        Visibility="Collapsed">
    <UIElement.Projection>
        <PlaneProjection RotationY="-90" />
    </UIElement.Projection>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"></ColumnDefinition>
            <ColumnDefinition Width="100"></ColumnDefinition>
            <ColumnDefinition Width="100"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"></RowDefinition>
            <RowDefinition Height="100"></RowDefinition>
            <RowDefinition Height="100"></RowDefinition>
        </Grid.RowDefinitions>
        <Button Grid.Column="2" Grid.Row="1" Click="RotateRight_Click">
            <Button.Content>
                <StackPanel>
                    <TextBlock HorizontalAlignment="Center">Rotate Right</TextBlock>
                    <TextBlock HorizontalAlignment="Center">To</TextBlock>
                    <TextBlock HorizontalAlignment="Center">Side 4</TextBlock>
                </StackPanel>
            </Button.Content>
        </Button>
        <Button Grid.Column="0" Grid.Row="1" Click="RotateLeft_Click">
            <Button.Content>
                <StackPanel>
                    <TextBlock HorizontalAlignment="Center">Rotate Left</TextBlock>
                    <TextBlock HorizontalAlignment="Center">To</TextBlock>
                    <TextBlock HorizontalAlignment="Center">Side 2</TextBlock>
                </StackPanel>
            </Button.Content>
        </Button>
        <TextBlock HorizontalAlignment="Center" 
                   VerticalAlignment="Center" 
                   Grid.Column="1" 
                   Grid.Row="1" 
                   Text="Side 6">
        </TextBlock>
    </Grid>
</Border>

这是我正在使用的 C# 代码,也许你可以看到我做错了什么?

public static void panelMain(string strPassGridName, System.Windows.Media.Color mcPassColor, 
int intRowProperty, int intColumnProperty, Visibility vVisibility, 
string[] strButtonTitles, Grid passLayoutRoot, Canvas passCanvas)
    {
        Grid panelGrid = new Grid();

             panelGrid.Name = strPassGridName;
             panelGrid.Background = new SolidColorBrush(mcPassColor);

             panelGrid.SetValue(Grid.RowProperty, intRowProperty);
             panelGrid.SetValue(Grid.ColumnProperty, intColumnProperty);
             panelGrid.Visibility = vVisibility;

        RowDefinition row1 = new RowDefinition();
                      row1.Height = new GridLength(100, GridUnitType.Auto);
             panelGrid.RowDefinitions.Add(row1);

        ColumnDefinition column1 = new ColumnDefinition();
                         column1.Width = new GridLength(100);
             panelGrid.ColumnDefinitions.Add(column1);


        passLayoutRoot.Children.Add(panelGrid);
    }
4

2 回答 2

1

我想通了,我需要先创建边框,然后将网格添加到边框。一个主要区别是我不能直接引用边框对象,我需要“找到它”

   Border findBorder = passLayoutRoot.FindName("bd" + strPassGridName) as Border;
   if (findBorder == null)
   { }
   else
   {
     findBorder.Child = panelGrid;
   }

这工作得很好....感谢所有试图提供帮助的人

于 2012-04-19T13:59:43.643 回答
0

你可以这样做,如下所示,

        Border gridBorder = new Border();
        gridBorder.BorderBrush = new SolidColorBrush(Colors.Black);
        gridBorder.BorderThickness = new Thickness(4);
        gridBorder.Child = new Grid(); //Your grid here
        LayoutRoot.Children.Add(border); // ParentGrid(layout) holding the border
于 2012-04-18T05:56:23.377 回答