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?
相关问题
- VNC control for WPF application
- Tkinter Grid Columnspan ignored
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
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.
It looks like this:
The
Background
color can just be set for the entireGrid
by using theBackground
property: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 theShowGridLines
property, which can be used to show thin dotted lines that cannot be styled.Per MSDN:
So in order to add borders to your Grid, you have to add
Border
elements or controls that contain aBorder
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.it depends on what you're intending to do with this Grid but I assume you want to populate the Grid's cells with controls.
You'll have to set Background and border(Stroke) properties on the controls then or encapsulate each of your controls in a Border first.
But, of course if you want the same background color for each cell, then set the background of the Grid. :)
i hope i've answered well.