5

我正在尝试在需要以编程方式添加的 StackPanel 后面的 C# 代码中设置以下属性:

BorderThickness
BorderBrush

关于如何以编程方式设置这些的任何想法?

4

3 回答 3

25

我知道这是一年后的事,但我已经找到了答案,以防有人仍然需要它。

这是一个完整的例子

// Create a StackPanel and Add children
StackPanel myStackPanel = new StackPanel();
Border myBorder1 = new Border();
myBorder1.Background = Brushes.SkyBlue;
myBorder1.BorderBrush = Brushes.Black;
myBorder1.BorderThickness = new Thickness(1);
TextBlock txt1 = new TextBlock();
txt1.Foreground = Brushes.Black;
txt1.FontSize = 12;
txt1.Text = "Stacked Item #1";
myBorder1.Child = txt1;

Border myBorder2 = new Border();
myBorder2.Background = Brushes.CadetBlue;
myBorder2.Width = 400;
myBorder2.BorderBrush = Brushes.Black;
myBorder2.BorderThickness = new Thickness(1);
TextBlock txt2 = new TextBlock();
txt2.Foreground = Brushes.Black;
txt2.FontSize = 14;
txt2.Text = "Stacked Item #2";
myBorder2.Child = txt2;

// Add the Borders to the StackPanel Children Collection
myStackPanel.Children.Add(myBorder1);
myStackPanel.Children.Add(myBorder2);
mainWindow.Content = myStackPanel;
于 2013-09-05T13:23:12.623 回答
1

StackPanel 没有 BorderThickness 或 BorderBrush 属性。只有背景。如果要设置这些,则需要将 StackPanel 包装在边框控件中:

<Border  x:Name="StackBorder">
<StackPanel>
</Border>

然后您可以致电:

StackBorder.BorderThickness = new Thickness(1);
StackBorder.BorderBrush = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.Red);
于 2012-09-06T02:25:29.187 回答
0

您不能在StackPanel对象本身上设置边框属性。您将 StackPanel 对象放置在Border对象中,并使用以下内容在 Border 对象上设置BorderThicknessBorderBrush属性:

myBorder.BorderBrush = Brushes.Black;
myBorder.BorderThickness = new Thickness(1);
于 2012-09-06T02:21:35.130 回答