我正在尝试在程序中动态添加StackPanel并设置以下属性,具体的C#代码如下:
有没有想法如何通过程序进行设置?
BorderThickness
BorderBrush
有没有想法如何通过程序进行设置?
BorderThickness
BorderBrush
我知道这是一年后了,但如果有人仍需要,我已经找到了答案。
// 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;
<Border x:Name="StackBorder">
<StackPanel>
</Border>
您可以随后调用:
StackBorder.BorderThickness = new Thickness(1);
StackBorder.BorderBrush = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.Red);
您不能在 StackPanel 对象本身上设置边框属性。您需要将 StackPanel 对象放置在 Border 对象内,并使用以下类似的方式在 Border 对象上设置 BorderThickness
和 BorderBrush
属性:
myBorder.BorderBrush = Brushes.Black;
myBorder.BorderThickness = new Thickness(1);