How to customize the GridSplitter
I am migrating a WinForms form to WPF and I need to draw a gridsplitter as follows. This is my WinForms implementation:
It's easy to implement in WinForms. I inherited from the Splitter class and just overrides the method OnPaint()
.
Now I'm trying to figure out how to proceed. I don't know how to use the control pattern in XAML because I need to re-draw the shapes when the user moves the scroll. I seem to need to write the code behind, but I don't know how to proceed.
How can I proceed? A simple example, like drawing a line from (0,0)
to (gridsplitter.right, gridsplitter.bottom)
, would help.
source to share
Finally, I created a class generated by Canvas to do the rendering:
public class DiffSplitterCanvas : Canvas
{
public DiffSplitterCanvas()
{
this.SnapsToDevicePixels = true;
}
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
// draw your stuff here
}
}
And call it like control template from XAML code:
<GridSplitter Grid.Column="1" Width="50" HorizontalAlignment="Stretch">
<GridSplitter.Template>
<ControlTemplate TargetType="{x:Type GridSplitter}">
<custom:DiffSplitterCanvas/>
</ControlTemplate>
</GridSplitter.Template>
</GridSplitter>
source to share
You can try painting before WriteableBitmap
and use that as a background for the divider. This may require rewriting the splitter template to get things right. I'm not sure how effective scrolling will be.
WriteableBitmap only provides low level pixel access, if you want higher level features like drawing lines and circles you could look into a library like WriteableBitmapEx which extends the class with GDI like drawing commands.
Code for setting the background of the GridSplitter:
<GridSplitter x:Name="gridSplitterTreeNodes" Width="5" BorderThickness="1,0"
Cursor="SizeWE" RenderTransformOrigin="-1.2,0.507" ShowsPreview="True"
Style="{DynamicResource GridSplitterStyle1}">
<GridSplitter.Background>
<ImageBrush ImageSource="Images\gripDots.png" TileMode="FlipXY"
Stretch="UniformToFill"/>
</GridSplitter.Background>
</GridSplitter>
You want to set ImageBrush to your WritableBitmap
.
We draw a simpleLine with WriteableBitmapEx
:
writeableBmp.DrawLine(1, 2, 30, 40, Colors.Green);
source to share