2012-09-06 49 views

回答

0

您不能在StackPanel對象本身上設置邊框屬性。你把一個Border對象內部的StackPanel對象,並使用類似設置邊框對象的BorderThicknessBorderBrush屬性:

myBorder.BorderBrush = Brushes.Black; 
myBorder.BorderThickness = new Thickness(1); 
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); 
+0

好主意...唯一的問題是,我將有StackPanels的「X」號,所以我需要在代碼中創建所有的人身後。我能夠以編程方式創建Border和StackPanel對象,我似乎無法找到將StackPanel添加到邊框的正確語法。有任何想法嗎? – iTrout

+0

@iTrout這聽起來像你應該使用ItemsControl而不是在代碼中創建東西。 – mydogisbox

+0

@mydogisbox - 我正在使用Callisto Flyout,所以我需要在代碼中構建它。如果我能用XAML加載Callisto Flyout,那將是夢想成真! – iTrout

21

我知道這是一年過去了,但我發現如果有人仍然需要它的答案。

Here is a complete example

// 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; 
相關問題