Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawingVisual is not refreshed
    text
    copied!<p>I create my own <code>FrameworkElement</code> and override <code>VisualChildrenCount{get;}</code> and <code>GetVisualChild(int index)</code> by returning my own <code>DrawingVisual</code> instance. </p> <p>If I modify the content of the visual after initial rendering (e.g. in timer handler) using <code>DrawingVisual.RenderOpen()</code> and drawing into the context, the element is not refreshed.</p> <p>Here's the simplest sample:</p> <pre><code>using System; using System.Windows; using System.Windows.Media; using System.Windows.Threading; namespace VisualTest { public class TestControl : FrameworkElement { private readonly DrawingVisual _visual = new DrawingVisual(); public TestControl() { Draw(false); var timer = new DispatcherTimer {Interval = new TimeSpan(0, 0, 2)}; timer.Tick += (sender, args) =&gt; { Draw(true); InvalidateVisual(); timer.Stop(); }; timer.Start(); } protected override Visual GetVisualChild(int index) { return _visual; } protected override int VisualChildrenCount { get { return 1; } } private void Draw(bool second) { DrawingContext ctx = _visual.RenderOpen(); if (!second) ctx.DrawRoundedRectangle(Brushes.Green, null, new Rect(0, 0, 200, 200), 20, 20); else ctx.DrawEllipse(Brushes.Red, null, new Point(100, 100), 100, 100); ctx.Close(); } } } </code></pre> <p><code>InvalidateVisual()</code> does nothing. Although if you resize the window containing the element, it gets updated.</p> <p>Any ideas on how to properly refresh the content? Preferably <strong>without</strong> introducing new dependency properties for my element.</p>
 

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