从示例创建 wpf UIElement
我希望创建一堆矩形,它们将共享一些属性,而其他一些属性将有所不同。这一切都是在代码隐藏中完成的,显然,无需通过复制和粘贴技巧就可以做到这一点,但本着让我的代码更加优雅的精神;是否可以有一个像这样的示例矩形
Rectangle sampleRect = new Rectangle(){Stroke = strokebrush,Margin = new Thickness(5)};
,并在之后使用不同的高度和宽度属性对所有其他矩形进行建模?
更新感谢您的回答,我实际上正在寻找更多 CSS/样式的东西......
I'm looking to create a bunch of rectanges that are going to share some properties and some other properties will be different. This is all done in codebehind, and clearly it is very possible to do this without breaking a sweat by copy and paste skills, but in the spirit of making my code more elegant; is it possible to have a sample rectangle like so
Rectangle sampleRect = new Rectangle(){Stroke = strokebrush,Margin = new Thickness(5)};
and model everyother rectangle after that with diefferent height and width attributes?
UPDATE Thanks for the answers, I am actually looking for more of a CSS/style thing...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将其包装在一个方法中,如下所示(假设 strokebrush 是某种本地字段)
You could wrap it inside a method, like this (assuming strokebrush is some sort of local field)
您可以拥有代表
Rectangle
参数的类,并使用DataTemplate
将您的类转换为 XAML 中的Rectangle
,并且您的类将具有默认的 Strock 和边距,您可以覆盖高度和宽度
you could have Class that represents your
Rectangle
parameters and useDataTemplate
to convert your class intoRectangle
in your XAMLand your class will have default Strock and Margin and you can override height and width