How to set row border and background color in WPF Grid
grid, wpf, wpf-controls
Solution
The `Background` color can just be set for the entire `Grid` by using the `Background` property:
<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.
As for Borders, a `Grid` only contains the `ShowGridLines` property, which can be used to show thin dotted lines that cannot be styled.
Per 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.
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.
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">
Problem
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?