2011-05-13 39 views
0

我正在創建一堆要共享某些屬性的矩形,而其他一些屬性會有所不同。這一切都是在代碼隱藏的情況下完成的,很明顯,通過複製和粘貼技巧,我們可以做到這一點,但本着讓代碼更優雅的精神;是有可能有像這樣從示例中創建一個wpf UIElement

Rectangle sampleRect = new Rectangle(){Stroke = strokebrush,Margin = new Thickness(5)}; 

和模型everyother矩形樣品矩形後,與diefferent height和width屬性?

UPDATE感謝您的答案,我實際上是尋找更多的CSS /樣式的事情...

回答

0

,你可以有一個代表你Rectangle參數類,並使用DataTemplate到類轉換爲Rectangle在你的XAML

,你的類將有默認的撫摩和保證金,你可以覆蓋的高度和寬度

0

你可以把它包裝的方法裏面,像這樣的(假設strokebrush是某種羅cal field)

private static Rectangle RectangleBuilder(int height, int width) 
{ 
    Rectangle sampleRect = new Rectangle() 
    { 
     Stroke = strokebrush, 
     Margin = new Thickness(5), 
     Height = height, 
     Width = width 
    }; 
    return sampleRect; 
} 
相關問題