Silverlight:在网格周围添加边框

时间:2012-04-17 17:32:07

标签: c# silverlight grid border

我有一个网格,我需要放入边框,通过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);
    }

2 个答案:

答案 0 :(得分:1)

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

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

这很完美...... 感谢所有尝试提供帮助的人

答案 1 :(得分: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