Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: Drawing canvas content to a bitmap problem
    primarykey
    data
    text
    <p>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:</p> <pre><code>&lt;Style x:Key="DottedStyle" TargetType="TreeBuilder:DottedCanvas"&gt; &lt;!--Makes canvas dotted--&gt; &lt;Setter Property="Background" Value="{StaticResource DottedBrush}"/&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="DottedEnabled" Value="False"&gt; &lt;!--Removes dots when printing--&gt; &lt;Setter Property="Background" Value="White"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>The TextBox controls are wrapped to a UserControl and also have their own style:</p> <pre><code>&lt;Style x:Key="NodeBoxStyle" TargetType="{x:Type TreeBuilder:TreeNodeBox}"&gt; &lt;Setter Property="BorderThickness" Value="1"/&gt; &lt;Style.Triggers&gt; &lt;!--Must remove d--&gt; &lt;Trigger Property="IsBeingPrinted" Value="true"&gt; &lt;Setter Property="BorderThickness" Value="0"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>Then I'm trying to print this canvas to a bitmap:</p> <pre><code>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&lt;IPrintable&gt;()) { element.IsBeingPrinted = false; } } private void PrepareTree(Canvas canvas) { canvas.Focus(); canvas.DottedEnabled = false; foreach (var element in canvas.Children.OfType&lt;IPrintable&gt;()) { element.IsBeingPrinted = true; } } </code></pre> <p>The image looks so:</p> <p><a href="http://i.stack.imgur.com/7uKUs.png" rel="nofollow">Result</a></p> <p>The Canvas has no dots (the style did its work) but the TextBox's BorderThickness stays 1 on the image. A breakpoint in <code>PrepareTree()</code> 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? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload