OnRender Method does not work with more than 144 controls
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 http://s3.postimage.org/id6jvq09n/render_Test_Example.png
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.csYour approach should be the one suggested by chibacity. This type of behavior is standard and is even used by the DataGridTextColumn
that ships with WPF. From the MSDN:
DataGridTextColumn creates a TextBlock element in the non-editing mode and a TextBox element in the editing mode.
Also, as suggested by many other users in comments, you should not override OnRender
to adjust the visual appearance of a control. In WPF, changes to a control's visual appearance can be accomplished by adjusting the control's Style
and/or Template
. The following style results in the exact same appearance change as your OnRender
override:
<Style TargetType="TextBox">
<Setter Property="BorderBrush" Value="Red" />
</Style>
You should only "derive and override" when you're extending the functionality and/or purpose of a control and there's nothing in your example that suggests that's what you're doing.
Additionally, your RenderTestPanel.xaml implies that all you're doing is creating a data grid which is provided by WPF. I would strongly suggest using the out-of-the-box DataGrid
, styling the DataGridTextColumn
and you'll (probably) accomplish your goals with less code and entirely in XAML.
I was able to work around a very similar problem to this. I posted the resolution here: https://stackoverflow.com/a/40605635/5823234
精彩评论