2011-05-13 37 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; 
} 
相关问题