How to redraw or refresh in OnRender?

前端 未结 4 768
臣服心动
臣服心动 2021-01-01 03:18

I want to draw something dynamically. Following code shows my OnRender. I\'m setting DrawItem somewhere in my program where I need it. But when I\'m calling DrawItem =

相关标签:
4条回答
  • 2021-01-01 03:45

    Your question is not clear but from what I can understand you seem to be asking for a method to refresh the visuals after updating your drawing variables. You could use the following after updating your drawing variables:

    this.InvalidateVisual();
    

    and with the property:

    private int drawItem;
    pulic int DrawItem
    {
     get {return drawItem;}
     set 
     {
        drawItem=value;
        this.InvalidateVisual();
     }
    }
    
    0 讨论(0)
  • 2021-01-01 03:45

    If the size of your control changes, you can use InvalidateVisual(), however keep in mind this causes a fairly expensive re-layout of your UI. If the size of your control is staying the same, you shouldn't call InvalidateVisual().

    A more efficient way to update your UI is to create a DrawingGroup "backing store" and add it to the DrawingContext during OnRender(). You can then update it whenever you like, using DrawingGroup.Open(), and WPF will update your UI.

    If this sounds confusing, remember that WPF is a retained drawing system. That means OnRender() might better be called AccumulateDrawingObjects(). It's actually accumulating a tree of live drawing objects, some of which (like DrawingGroup, RenderTargetBitmap, and WriteableBitmap) can be updated later.

    This is what it looks like:

    DrawingGroup backingStore = new DrawingGroup();
    
    protected override void OnRender(DrawingContext drawingContext) {      
        base.OnRender(drawingContext);            
    
        Render(); // put content into our backingStore
        drawingContext.DrawDrawing(backingStore);
    }
    
    // I can call this anytime, and it'll update my visual drawing
    // without ever triggering layout or OnRender()
    private void Render() {            
        var drawingContext = backingStore.Open();
        Render(drawingContext);
        drawingContext.Close();            
    }
    
    0 讨论(0)
  • 2021-01-01 03:52

    I ran into similar issue and found an even more elegant solution. Define a DependencyProperty like so:

    public static readonly DependencyProperty PropertyNameProperty = DependencyProperty.Register("PropertyName", typeof(PropertyType), typeof(PropertyElementType), new FrameworkPropertyMetadata(DefaultPropertyValue, FrameworkPropertyMetadataOptions.AffectsRender));
    

    The key here is this:

    FrameworkPropertyMetadataOptions.AffectsRender
    

    So now the property can be set in XAML and updates visual automatically on changing.

    0 讨论(0)
  • 2021-01-01 03:54

    http://msdn.microsoft.com/de-de/library/ms598919(v=vs.100).aspx

    You have to call InvalidateVisual for the Control that shall be updated. But as you can read, a call to InvalidateVisual causes a complete layout cycle, so be sure for performance reasons that your canvas or whatever you want to draw on contains no or only a few child elements.

    0 讨论(0)
提交回复
热议问题