设置 WPF 布局网格背景(每个单元格、行、列)的样式
我想知道是否有任何方法可以设置 WPF 布局网格的单元格、行和列的样式。 我一直在尝试寻找任何信息,但我发现的少数提及内容并没有那么丰富。
我想将网格样式设置为看起来像链接的屏幕截图中的网格。
如果实际控件不支持它,我可以以某种方式继承它并执行吗? 我对 WPF 还很陌生,因此非常感谢任何帮助。
另一件事,我知道我可以对网格中的每个控件进行样式设置,但这似乎有点矫枉过正。 我想要一个可以自己完成的网格。
I would like to know if there is any way to style a WPF layout grid's cells, rows and columns. I've been trying to find any information and the few mentions I've found have not been that informative.
I would like to style the grid to look like the one in the linked screenshot.
If the actual control does not support it, can I inherit it somehow and do it then? I am quite new to WPF so any help would be very appreciated.
One other thing, I know I can style each and every control within the grid, but it seems like overkill. I would like to have a grid that does it itself.
screenshot http://img21.imageshack.us/img21/2842/capturehz8.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
@Dan 推荐我目前正在阅读的 WPF Unleashed。 就在今天早上,我看到了一个解决您问题的部分。
第 6 章,第 161 页:
顺便说一句,WPF 释放了岩石。 它写得很好,全彩印刷使其更易于阅读。
@Dan recommends WPF Unleashed, which I'm currently reading. Just this morning, I come across a section addressing your question.
Chapter 6, Page 161:
Btw, WPF Unleashed rocks. Its very well written, and the print in full color makes it even more easier to read.
这是一个快速(非常粗略的示例),您可以修改它以获得您想要的格式(如果您认真使用 WPF,您会发现 Blend 对使您的布局看起来不错有很大帮助):
Here's a quick (very rough sample) that you could hack around to get the format you want (if you're serious about working with WPF, you'll find Blend an enormous help in getting your layouts looking good):
WPF
Grid
本身没有可见单元格。 将它们视为不可见的网格线,您可以将子元素对齐。因此,要设置网格单元格的样式,您必须设置网格内对齐的项目的样式。
将
Grid
视为类似于 WinFormsDataGrid
的东西会令人困惑。 我猜想它最接近的 WinForms 等效项是TableLayout
控件。查看一些第 3 方网格控件。 我在 DevExpress 处于测试阶段时使用过它,发现它非常简单。
The WPF
Grid
doesn't have visible cells as such. Think of them as invisible grid lines against which you can have child elements be aligned.So, to style the grid's cells, you have to style the items that are aligned inside the grid.
It is confusing to think of the
Grid
as being anything like a WinFormsDataGrid
. I guess its closest WinForms equivalent is theTableLayout
control.Check out some 3rd party grid controls. I used the DevExpress one while it was in beta and found it pretty straightforward.
我建议您使用边框来设计样式。
您可以通过为每行和每列创建边框并相应地设置行跨度和列跨度来非常轻松地重新创建该布局。
您将有 5 个 colspan 2 的边框,这些边框将处理每行的渐变背景以及每行顶部和底部的边框。 然后你将有 2 个行距为 5 的边框,它们将处理列边框。 想象一下,您正在叠加边框以形成您想要的视觉网格效果。
对于标题和外边框,只需根据需要用边框和样式包裹整个网格即可。
我建议将您的样式存储为资源,以便您可以将所有样式信息保存在一个位置。
请注意学习样式的工作原理,因为它非常强大,但有一个学习曲线,因为它与 CSS 的工作方式完全不同。 如果可以的话,我建议您阅读 WPF Unleashed。
I would recommend using borders for your styling.
You could recreate that layout pretty easily by creating borders for each row and each column and set the rowspans and colspans accordingly.
You will have 5 borders with colspan 2, these borders will take care of your gradient backgrounds for each row and the borders along the top and bottom of each row. Then you will have 2 borders with rowspan 5 these will handle the column borders. Imagine that you are overlaying the borders to form the visual grid effect you are after.
For the header and outer border, just wrap the entire grid with a border and style as needed.
I would recommend storing your styles as resources so you can keep all your styling info in one place.
Take care to learn how the styling works because it is pretty powerful, but there is a learning curve as it is quite different to the way CSS works. I would recommend reading WPF Unleashed if you can.
我在寻找为 DataGrid 单元格设置边距(或填充)的方法时发现了这篇文章。 我的问题得到了解决,感谢发布在(接近尾声)的示例 xaml 代码——非常简约。
http://forums.silverlight.net/forums/p/16842/55997。 ASPX
I found this post when looking for method for setting margin (or padding) for DataGrid cells. My problem was solved thanks to example xaml code posted at (near the end) -- pretty minimalistic.
http://forums.silverlight.net/forums/p/16842/55997.aspx