如何在 WPF Grid 中设置行边框和背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11244457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set row border and background color in WPF Grid
提问by Buzz
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?
我们如何在 WPF 网格控件中设置边框和背景颜色,
我正在动态创建行和列,然后添加到网格中,
我们可以从后面的代码中设置颜色和边框吗?
采纳答案by Rachel
The Background
color can just be set for the entire Grid
by using the Background
property:
该Background
颜色正好可以为整个组Grid
通过使用Background
属性:
<Grid Background="Red" />
Or if you want it set for individual cells, you need to add an element to the cell that has its Background
property set.
或者,如果您希望为单个单元格设置它,则需要向已Background
设置其属性的单元格添加一个元素。
As for Borders, a Grid
only contains the ShowGridLines
property, which can be used to show thin dotted lines that cannot be styled.
对于 Borders,aGrid
只包含ShowGridLines
属性,可用于显示无法设置样式的细虚线。
Per MSDN:
每 MSDN:
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.
仅虚线可用,因为此属性旨在用作调试布局问题的设计工具,而不是用于生产质量代码。如果您希望在 Grid 内设置线条,请将 Grid 内的元素设置为具有边框的样式。
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 单元格添加Border
包含 a 的元素或控件Border
,并为这些元素设置样式。
But there is an alternative. This blog postoutlines 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 pastwhen I wanted to render grid lines, but didn't want to fill every cell with an object.
但还有一个选择。这篇博文概述了如何扩展 Grid 类以创建具有Grid
线条属性的自定义 Grid 。过去,当我想渲染网格线但不想用对象填充每个单元格时,我已经成功地使用了它。
<my:CustomGrid ShowCustomGridLines="True"
GridLineBrush="Blue"
GridLineThickness="1">
回答by Christopher Scott
Here is a bit of a hack that seems to work well. If you place a background element in the rows / columns along with the elements you normally would place there, it will act as a background. You'll just need to mind the ordering of the elements in the XAML (the elements appear in increasing Z-Order), or set the Panel.Zorder accordingly.
这里有一个看起来运行良好的 hack。如果您在行/列中放置一个背景元素以及您通常放置在那里的元素,它将充当背景。您只需要注意 XAML 中元素的顺序(元素以递增的 Z-Order 出现),或相应地设置 Panel.Zorder。
<Window x:Class="gridBackground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Background="Red" />
<Border Grid.Row="2" Grid.Column="1" Background="Red" />
<Border Grid.Row="1" Background="LightBlue" />
<Border Grid.Row="2" Background="Orange" />
<Border Grid.Row="0" Grid.Column="1" Background="Orange" />
<TextBlock Grid.ColumnSpan="2" Grid.Row="1" Text="Here is some more text" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.ColumnSpan="2" Text="Here is some text" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.ColumnSpan="2" Grid.Row="2" Text="Here is even more text" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
It looks like this:
它看起来像这样: