Reputation: 91
I have a custom control of class DottedCanvas inherited from Canvas, which contains some custom controls of class TreeNode inherited from TextBox. The canvas control has style:
<Style x:Key="DottedStyle" TargetType="TreeBuilder:DottedCanvas">
<!--Makes canvas dotted-->
<Setter Property="Background" Value="{StaticResource DottedBrush}"/>
<Style.Triggers>
<Trigger Property="DottedEnabled" Value="False">
<!--Removes dots when printing-->
<Setter Property="Background" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
The TextBox controls are wrapped to a UserControl and also have their own style:
<Style x:Key="NodeBoxStyle" TargetType="{x:Type TreeBuilder:TreeNodeBox}">
<Setter Property="BorderThickness" Value="1"/>
<Style.Triggers>
<!--Must remove d-->
<Trigger Property="IsBeingPrinted" Value="true">
<Setter Property="BorderThickness" Value="0"/>
</Trigger>
</Style.Triggers>
</Style>
Then I'm trying to print this canvas to a bitmap:
public BitmapSource BuildImage(Tree tree)
{
canvas = tree.Canvas;
PrepareTree(canvas);
Size size = GetSize();
canvas.Measure(size);
canvas.Arrange(new Rect(size));
RenderTargetBitmap image = new RenderTargetBitmap(
(int)size.Width,
(int)size.Height,
96,
96,
PixelFormats.Pbgra32);
image.Render(canvas);
UnprepareTree(canvas);
return image;
}
private void UnprepareTree(Canvas canvas)
{
canvas.DottedEnabled = true;
foreach (var element in canvas.Children.OfType<IPrintable>())
{
element.IsBeingPrinted = false;
}
}
private void PrepareTree(Canvas canvas)
{
canvas.Focus();
canvas.DottedEnabled = false;
foreach (var element in canvas.Children.OfType<IPrintable>())
{
element.IsBeingPrinted = true;
}
}
The image looks so:
The Canvas has no dots (the style did its work) but the TextBox's BorderThickness stays 1 on the image. A breakpoint in PrepareTree()
function shows that TextBox BorderThickness property is set to zero (that means, the style works too).
How can I get the "new" version of TextBoxes on the image?
Upvotes: 1
Views: 769
Reputation: 91
I succeed to solve the problem. I called Measure()
and Arrange()
methods on Canvas, that's why it rendered before printing. Making the same with TextBoxes helped.
Upvotes: 1