在代码后面创建一个silverlight边框?

时间:2011-06-10 01:44:20

标签: c# .net silverlight grid code-behind

我在XAML中有以下边框:

<Border
    Grid.Column="0"
    Grid.ColumnSpan="3"
    Grid.RowSpan="3"
    CornerRadius="1,1,1,1"
    Background="Red"
    BorderBrush="#333333"
    BorderThickness="1,1,1,1"
    x:Name="border"
    RenderTransformOrigin="0.5,0.5">
    <Border.RenderTransform>
        <TransformGroup>
            <ScaleTransform />
            <SkewTransform />
            <RotateTransform />
            <TranslateTransform />
        </TransformGroup>
    </Border.RenderTransform>
    <ContentPresenter
        x:Name="contentPresenter"
        Margin="10,0,10,0"
        ContentTemplate="{TemplateBinding ContentTemplate}"
        VerticalAlignment="Center"
        HorizontalAlignment="Center" />
</Border>

我试图在代码后面创建一个类似的边框(C#)。我不能超越Border b = new Border(),我不确定我应该如何将边框放在特定的网格列中或如何跨越它。

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

这样的事情:

var border = new Border();

Grid.SetColumn(border, 0);
Grid.SetColumnSpan(border, 3);
Grid.SetRowSpan(border, 3);

border.CornerRadius = new CornerRadius(1);
border.Background = new SolidColorBrush(Colors.Red);
border.BorderBrush = new SolidColorBrush(Color.FromArgb(0xff, 0x33, 0x33, 0x33));
border.BorderThickness = new Thickness(1);
border.RenderTransformOrigin = new Point(0.5, 0.5);

var transformGroup = new TransformGroup();
transformGroup.Children.Add(new ScaleTransform());
transformGroup.Children.Add(new SkewTransform());
transformGroup.Children.Add(new RotateTransform());
transformGroup.Children.Add(new TranslateTransform());
border.RenderTransform = transformGroup;

如果您希望我设置其余属性,请告诉我。

答案 1 :(得分:2)

如果可以帮到你:

            Border b = new Border();
            Grid.SetColumn(b, 0);
            Grid.SetColumnSpan(b, 3);
            Grid.SetRowSpan(b, 3);

            b.CornerRadius = new CornerRadius(1);
            b.Background = new SolidColorBrush(Colors.Red);

            // Then add your border to the grid
            g.Children.Add(b);

但对于ContentPresenter,我不知道该怎么做