【问题标题】:Silverlight: Add border around gridSilverlight:在网格周围添加边框
【发布时间】:2014-04-28 19:13:58
【问题描述】:

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

【问题讨论】:

  • 必填项:你能告诉我们你在 C# 中的尝试吗?
  • 你看到Child属性了吗?
  • 是的,这就是告诉我它已经是一个孩子的地方

标签: c# silverlight grid border


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    你可以这样做,

            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
    

    【讨论】:

    • 抱歉没用,我得到“元素已经是另一个元素的子元素。”如果我稍微改变最后一行,我会在我的网格中得到一个正方形有一个边框 Border gridBorder = new Border(); gridBorder.BorderBrush = new SolidColorBrush(Colors.Black); gridBorder.BorderThickness = 新厚度(4); //gridBorder.Child = panelGrid; //这里是你的网格 panelGrid.Children.Add(gridBorder);
    • 你的最后一行 panelGrid.Children.Add(gridBorder);不会工作。您需要将 gridBorder 添加为根网格的子级,如下所示。 LayoutRoot.Children.Add(border);
    • 我一定遗漏了一些东西,如何让边框环绕网格?这条线 (gridBorder.Child = new Grid();) 说要添加新网格,但我如何引用正在创建的网格
    • 这是我正在使用的c#,也许你能看到我做错了什么?
    猜你喜欢
    • 2014-09-12
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多