locked
OnRender Method does not work with more than 144 controls RRS feed

  • Question

  • I have a custom TextBox which overrides the OnRender method. Unfortunately, OnRender does not work properly when I add more than 143-145 TextBoxes to the grid.

    This is what a windows with 160 TextBoxes looks like in the wpf designer. Each TextBox sets the border brush to red in the OnRender Method. For the last column of textboxes, OnRender does not work anymore.

    render test example

    The problem is not bound to the wpf designer, the same happens at runtime. Funnilly enough, if you delete one component inside the designer or at runtime once it has been rendered, then all the other controls suddenly work.

    example code:

    MytextBox.cs
    RenderTestPanel.xaml
    RenderTestPanel.xaml.cs

    I'd like to avoid any workarounds such as using TextBlocks and switching to TextBlocks if the field is focused.

    Monday, October 10, 2011 8:05 AM

Answers

  • Hi gauda,

    TextBox uses a ListBoxChrome object to render its border. And ListBoxChrome's BorderBrush property is data bound to the TextBox's BroderBrush property. The ListBoxChrome's BorderBrush property has the AffectsRender flag on it. Which means any changes to the value of this property will finally call the OnRender method.

    It's quite unusual to change the value of BorderBrush property in the OnRender method. OnRender method is where you finally renders the element, and BorderBrush indicates how to render it. This property is supposed to be set before OnRender method is called.

    In your code, OnRender method works and the BorderBrush is set to Red. But the last few TextBox is not properly rendered. If you try typing in it, the text will not update also.

    My suggestion is not to change the BorderBrush in OnRender method. If you want to draw a red border, you can use DrawingContext to draw a red rectangle directly.

    If for some reason you must change BorderBrush in OnRender method, please feel free to kindly elaborate it so that we can help you further.

    Best regards,

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Thursday, October 20, 2011 1:57 AM
    Monday, October 17, 2011 3:41 AM

All replies

  • Using TextBlock instead of TextBox does not work anyway. Just tried it.
    Monday, October 10, 2011 9:56 AM
  • Hi gauda,

    TextBox uses a ListBoxChrome object to render its border. And ListBoxChrome's BorderBrush property is data bound to the TextBox's BroderBrush property. The ListBoxChrome's BorderBrush property has the AffectsRender flag on it. Which means any changes to the value of this property will finally call the OnRender method.

    It's quite unusual to change the value of BorderBrush property in the OnRender method. OnRender method is where you finally renders the element, and BorderBrush indicates how to render it. This property is supposed to be set before OnRender method is called.

    In your code, OnRender method works and the BorderBrush is set to Red. But the last few TextBox is not properly rendered. If you try typing in it, the text will not update also.

    My suggestion is not to change the BorderBrush in OnRender method. If you want to draw a red border, you can use DrawingContext to draw a red rectangle directly.

    If for some reason you must change BorderBrush in OnRender method, please feel free to kindly elaborate it so that we can help you further.

    Best regards,

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Thursday, October 20, 2011 1:57 AM
    Monday, October 17, 2011 3:41 AM