C# 从示例创建wpf UIElement
我希望创建一组矩形,它们将共享一些属性,而其他一些属性将有所不同。这一切都是在codebehind中完成的,很明显,通过复制和粘贴技巧,在不费吹灰之力的情况下完成这一切是非常有可能的,但这是为了让我的代码更加优雅;有可能有一个这样的矩形样本吗C# 从示例创建wpf UIElement,c#,wpf,uielement,C#,Wpf,Uielement,我希望创建一组矩形,它们将共享一些属性,而其他一些属性将有所不同。这一切都是在codebehind中完成的,很明显,通过复制和粘贴技巧,在不费吹灰之力的情况下完成这一切是非常有可能的,但这是为了让我的代码更加优雅;有可能有一个这样的矩形样本吗 Rectangle sampleRect = new Rectangle(){Stroke = strokebrush,Margin = new Thickness(5)}; 然后用不同的高度和宽度属性对每个矩形进行建模 更新谢谢你的回答,我实际上在寻找
Rectangle sampleRect = new Rectangle(){Stroke = strokebrush,Margin = new Thickness(5)};
然后用不同的高度和宽度属性对每个矩形进行建模
更新谢谢你的回答,我实际上在寻找更多的CSS/风格的东西 您可以拥有表示
Rectangle
参数的类,并使用DataTemplate
将类转换为XAML中的Rectangle
你的类将有默认的Strock和Margin,你可以覆盖height和width,你可以把它包装在一个方法中,就像这样(假设strokebrush是某种局部字段)
private static Rectangle RectangleBuilder(int height, int width)
{
Rectangle sampleRect = new Rectangle()
{
Stroke = strokebrush,
Margin = new Thickness(5),
Height = height,
Width = width
};
return sampleRect;
}