Wednesday, May 7, 2008

Khao San Road Bikini Wax

own lines in Grid Panel draw


The grid is the most powerful and most frequently used panels in WPF.

The property ShowGridLines Show Guides.

Grid

ShowGridLines

= "True">



These lines serve only as an aid for the developer, who can thus detect the position of columns and rows.


Unfortunately you can not determine how these lines look like himself. Or anything yet?

What actually happens when the grid is set ShowGridLines = True?

WPF Controls can distinguish itself, in which they OnRender method override. As is the case with OnPaint in WinForms.


But the Grid is not made use of to draw the lines! Instead, the logical UI elements in the panel, an additional visual element of "grid line renderer added. This does not happen by ControlTemplate. (Templates also change the look so the VisualTree), special programmatically by calling AddVisualChild (visual) in the base class.


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.


Public


Class

GridLinesAdorner


Inherits Adorner


Private Grid As
System.Windows.Controls.Grid



'surrender to the ornamental element (in this grid)


Sub


New (
ByVal grid As

System.Windows.Controls.Grid)



MyBase . New (grid)

Me

. Grid grid =

End Sub





Private _pen
As
Pen



'pen and draw the lines can be


Public


Property Pen () As
Pen


Get

Return

_pen

End


Get


Set (ByVal
value
As Pen)

_pen = value
End

Set



End
Property




'draw the lines




Protected Overrides Sub
OnRender (

ByVal dc As
System.Windows.Media.DrawingContext)

If Pen Is Nothing

Then


Exit Sub

 

If Grid.ColumnDefinitions.Count > 0
Then


Dim
x As
Double


For i As

Integer
= 1
To

Grid.ColumnDefinitions.Count - 1


x = Grid.ColumnDefinitions(i).Offset
dc.DrawLine(Pen, New Point(x, 0),
New
Point(x, Grid.ActualHeight))


Next

End


If

 

If grid.RowDefinitions.Count > 0
Then


Dim
y As
Double


For i As

Integer
= 1
To

Grid.RowDefinitions.Count - 1


y = Grid.RowDefinitions(i).Offset
dc.DrawLine(Pen, New Point(0, y),
New
Point(Grid.ActualWidth, y))


Next

End


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)


Class Window1




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

Dim

Adorner As

New
GridLinesAdorner (Grid)


adorner.Pen = New Pen (Brushes.Black, 2)

'Adorner the Add Layer


layer.Add (Adorner)



End

Sub



End

Class


The result



who still surrounded by a frame around the grid would like to have, the need to pack only the grid in a border.


Note: As you can see the Adorner


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