如何在 WPF Grid 中设置行边框和背景颜色 [英] How to set row border and background color in WPF Grid
问题描述
我们如何在 WPF 网格控件中设置边框和背景颜色,
我正在动态创建行和列然后添加到网格中,
我们可以从后面的代码中设置颜色和边框吗?
How can we set border and background color in the WPF grid control ,
i am creating rows and column dynamically and adding then to grid,
can we set color and border from the code behind?
推荐答案
Background
的颜色可以通过Background<为整个
Grid
设置/code> 属性:
The Background
color can just be set for the entire Grid
by using the Background
property:
<Grid Background="Red" />
或者,如果您想为单个单元格设置它,您需要向设置了 Background
属性的单元格添加一个元素.
Or if you want it set for individual cells, you need to add an element to the cell that has its Background
property set.
对于Borders,Grid
只包含ShowGridLines
属性,可用于显示无法设置样式的细虚线.
As for Borders, a Grid
only contains the ShowGridLines
property, which can be used to show thin dotted lines that cannot be styled.
每个 MSDN:
只有虚线可用,因为此属性旨在作为设计工具来调试布局问题,并不打算用于生产质量代码.如果您想要网格内的线条,请设置样式Grid 中的元素具有边框.
Only dotted lines are available because this property is intended as a design tool to debug layout problems and is not intended for use in production quality code. If you want lines inside a Grid, style the elements within the Grid to have borders.
因此,为了给网格添加边框,您必须向网格单元格添加 Border
元素或包含 Border
的控件,并设置这些元素的样式.
So in order to add borders to your Grid, you have to add Border
elements or controls that contain a Border
to the Grid cells, and style those elements.
但是还有一个选择.这篇博文概述了如何扩展 Grid 类以创建具有 Grid
行属性的自定义 Grid.当我想渲染网格线但不想填充每个单元格时,我已经使用它过去成功有一个对象.
But there is an alternative. This blog post outlines how you can extend the Grid class to create a custom Grid that has properties for Grid
lines. I've used it successfully in the past when I wanted to render grid lines, but didn't want to fill every cell with an object.
<my:CustomGrid ShowCustomGridLines="True"
GridLineBrush="Blue"
GridLineThickness="1">
这篇关于如何在 WPF Grid 中设置行边框和背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!