Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6717135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:49:00+00:00 2026-05-26T08:49:00+00:00

We are doing custom drawing in a control subclass’s OnRender . This drawing code

  • 0

We are doing custom drawing in a control subclass’s OnRender. This drawing code is based on an external trigger and data. As such, whenever the trigger fires, we need to re-render the control based on that data. What we’re trying to do is find out how to force the control to re-render but without going through an entire layout pass.

As stated above, most answers I’ve seen revolve around invalidating the Visual which invalidates the layout which forces new measure and arrange passes which is very expensive, especially for very complex visual trees as ours is. But again, the layout does not change, nor does the VisualTree. The only thing that does is the external data which gets rendered differently. As such, this is strictly a pure rendering issue.

Again, we’re just looking for a simple way to tell the control that it needs to re-execute OnRender. I have seen one ‘hack’ in which you create a new DependencyProperty and register it with ‘AffectsRender’ which you just set to some value when you want to refresh the control, but I’m more interested in what’s going on inside the default implementation for those properties: what they call to affect that behavior.


Update:

Well, it looks like there isn’t any such call as even the AffectsRender flag still causes an Arrange pass internally (as per CodeNaked’s answer below) but I’ve posted a second answer that shows the built-in behaviors as well as a work-around to suppress your layout pass code from running with a simple nullable size as a flag. See below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:49:01+00:00Added an answer on May 26, 2026 at 8:49 am

    Ok, I’m answering this to show people why CodeNaked’s answer is correct, but with an asterisk if you will, and also to provide a work-around. But in good SO-citizenship, I’m still marking his as answered since his answer led me here.

    Update: I’ve since moved the accepted answer to here for two reasons. One, I want people to know there is a solution to this (most people only read the accepted answer and move on) and two, considering he has a rep of 25K, I don’t think he’d mind if I took it back! 🙂

    Here’s what I did. To test this, I created this subclass…

    public class TestPanel : DockPanel
    {
        protected override Size MeasureOverride(Size constraint)
        {
            System.Console.WriteLine("MeasureOverride called for " + this.Name + ".");
            return base.MeasureOverride(constraint);
        }
    
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize)
        {
            System.Console.WriteLine("ArrangeOverride called for " + this.Name + ".");
            return base.ArrangeOverride(arrangeSize);
        }
    
        protected override void OnRender(System.Windows.Media.DrawingContext dc)
        {
            System.Console.WriteLine("OnRender called for " + this.Name + ".");
            base.OnRender(dc);
        }
    
    }
    

    …which I laid out like this (note that they are nested):

    <l:TestPanel x:Name="MainTestPanel" Background="Yellow">
    
        <Button Content="Test" Click="Button_Click" DockPanel.Dock="Top" HorizontalAlignment="Left" />
    
        <l:TestPanel x:Name="InnerPanel" Background="Red" Margin="16" />
    
    </l:TestPanel>
    

    When I resized the window, I got this…

    MeasureOverride called for MainTestPanel.
    MeasureOverride called for InnerPanel.
    ArrangeOverride called for MainTestPanel.
    ArrangeOverride called for InnerPanel.
    OnRender called for InnerPanel.
    OnRender called for MainTestPanel.
    

    but when I called InvalidateVisual on ‘MainTestPanel’ (in the button’s ‘Click’ event), I got this instead…

    ArrangeOverride called for MainTestPanel.
    OnRender called for MainTestPanel.
    

    Note how none of the measuring overrides were called, and only the ArrangeOverride for the outer control was called.

    It’s not perfect as if you have a very heavy calculation inside ArrangeOverride in your subclass (which unfortunately we do) that still gets (re)executed, but at least the children don’t fall to the same fate.

    However, if you know none of the child controls have a property with the AffectsParentArrange bit set (again, which we do), you can go one better and use a Nullable Size as a flag to suppress the ArrangeOverride logic from re-entry except when needed, like so…

    public class TestPanel : DockPanel
    {
        Size? arrangeResult;
    
        protected override Size MeasureOverride(Size constraint)
        {
            arrangeResult = null;
            System.Console.WriteLine("MeasureOverride called for " + this.Name + ".");
            return base.MeasureOverride(constraint);
        }
    
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize)
        {
            if(!arrangeResult.HasValue)
            {
                System.Console.WriteLine("ArrangeOverride called for " + this.Name + ".");
                // Do your arrange work here
                arrangeResult = base.ArrangeOverride(arrangeSize);
            }
    
            return arrangeResult.Value;
        }
    
        protected override void OnRender(System.Windows.Media.DrawingContext dc)
        {
            System.Console.WriteLine("OnRender called for " + this.Name + ".");
            base.OnRender(dc);
        }
    
    }
    

    Now unless something specifically needs to re-execute the arrange logic (as a call to MeasureOverride does) you only get OnRender, and if you want to explicitly force the Arrange logic, simply null out the size, call InvalidateVisual and Bob’s your uncle! 🙂

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIView subclass that I am doing some custom drawing in. When
So, I'm doing some custom drawing in a UIControl subclass by overriding the drawRect:
I have a custom control that is doing a lot of 2D drawing straight
I'm doing custom drawing in datagridview cells and I have items that can vertically
I am doing a custom control (inherited from VisualBasic.PowerPacks.LineShape ), that should be painted
I can't seem to get a custom action working. I might be doing this
In Drupal you can create your own nodetype in a custom module. Doing this
I have a layer-hosting view set up like this in a custom NSView subclass:
I am doing some custom drawing a UITableViewCell that I have subclassed. In there
I'm doing the next: docToPrint.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize(Custom, (int)Math.Round(DocWidth / 0.254), (int)Math.Round(DocHeight / 0.254));

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.