The grid is the most powerful and most frequently used panels in WPF.
The property ShowGridLines Show Guides.
ShowGridLines
Unfortunately you can not determine how these lines look like himself. Or anything yet?
What actually happens when the grid is set ShowGridLines = True?
The grid line renderer is derived from DrawingVisual. DrawingVisual UIElement and thus have the same ancestor, "Visual". DrawingVisuals are much more efficient than UIElement, because it directly on the "screen" (WPF subsystem) are distinguished.
shows the VisualTree in XamlPad that to be logical Child - a button - found in the hierarchy - have a visual element - the grid line renderer is.
Option 1: A separate grid line renderer Leave
My first idea was to write your own grid line renderer. True to the model of the place in the grid renderer.
The problem is that it is not possible the renderer in the VisuallTree the grid insert. The method that would make this possible - AddVisualChild - is in fact protected. create
A separate grid, draw only the grid lines to me but was too expensive.
There must be a other way of expressing.
Option 2: Write a GridLinesAdorner
If there is no way to change from a UIElement inside without a separate control to derive, so you have to try it just from the outside. The so-called make Adorner.
Adorner "decorate" a UIElement. How that determines the Adorner. It is important that a Adorner will always be drawn to a UIElement. That is, the Adorner hidden beneath drawings.
GridLinesAdorner
Inherits Adorner
Private Grid As
System.Windows.Controls.Grid
'surrender to the ornamental element (in this grid)
New (
ByVal grid As
MyBase . New (grid)
. Grid grid =
Private _pen
As Pen
'pen and draw the lines can be
Property Pen () As
Pen
Get
_pen
Get
Set (ByVal
value
As Pen)
_pen = value
End
End
Property
'draw the lines
Protected Overrides Sub
OnRender (
ByVal dc As
System.Windows.Media.DrawingContext)
If Pen Is Nothing
Exit Sub
If Grid.ColumnDefinitions.Count > 0
Then
Dim
x As
Double
For i As
Integer = 1 To
x = Grid.ColumnDefinitions(i).Offset
dc.DrawLine(Pen, New Point(x, 0),
New Point(x, Grid.ActualHeight))
Next
If
If grid.RowDefinitions.Count > 0
Then
Dim
y As
Double
For i As
Integer = 1 To
y = Grid.RowDefinitions(i).Offset
dc.DrawLine(Pen, New Point(0, y),
New Point(Grid.ActualWidth, y))
Next
If
End
Sub
End
Class
OnRender now leads the way by drawing the grid lines. On the row and column definitions of the grid, the offset of each column are retrieved from the Grid. These serve as coordinator for the corresponding lines at these points to draw. bring
Applying Adorners
To Adorner used, we now need to adapt the code-behind file. Unfortunately, it is not possible to apply directly Adorner in XAML. (The class AdornerDecorator is only there for a layer to provide a decorative element on which a Adorner can be used, but not for the sender itself Adorner)
Private Sub
Window1_Loaded (ByVal
As
Object, ByVal e
As System.Windows.RoutedEventArgs) Handles Me .
Loaded 'layer for the element to get drawn in which the Adorner is
Dim layer As
AdornerLayer = AdornerLayer.GetAdornerLayer ( Me
. Grid)
'Adorner create with pen
Adorner As
New GridLinesAdorner (Grid)
adorner.Pen = New Pen (Brushes.Black, 2)
'Adorner the Add Layer
layer.Add (Adorner)
End
End
Class
The result
is drawn on the grid. Therefore, the edges of the grid located buttons are not visible completely. here will still need an appropriate margin to be applied to the buttons to allow the correct distance from the grid inside.
0 comments:
Post a Comment