Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF performance loading user controls
    primarykey
    data
    text
    <p>I’m working on a WPF application with a tabbed MDI interface. Basically, the user clicks a menu item and this creates and adds a new tab. A new 'MyUserControl' is created and set to be the tab's Content. Something like this:</p> <pre><code> private void MenuItem_OnClick(object sender, RoutedEventArgs e) { this.TabControl.Items.Add(new TabItem() { Content = new MyUserControl() }); } </code></pre> <p>MyUserControl is composed of several nested controls (approx. 8 controls). When this approach is wired up to the actual control, the performance is unacceptable.</p> <p>Unless I’m losing my mind, I’ve noticed that the performance hit seems to be much less when declaring the tab and content in xaml ahead of time and simply toggling the tab item's Visibility property:</p> <pre><code> &lt;Controls:TabControl x:Name="TabControl" Grid.Row="1"&gt; &lt;Controls:TabControl.Items&gt; &lt;Controls:TabItem x:Name="MyTabItem" Visibility="Collapsed"&gt; &lt;Controls:MyUserControl x:Name="MyUserControl" /&gt; &lt;/Controls:TabItem&gt; &lt;/Controls:TabControl.Items&gt; &lt;/Controls:TabControl&gt; </code></pre> <p>and</p> <pre><code> private void MenuItem_OnClick(object sender, RoutedEventArgs e) { this.MyTabItem.Visibility = Visibility.Visible; } </code></pre> <p>Can anyone explain this? Is it really more efficient to build the "visual tree" in xaml rather than programmatically? Or has the performance hit in my second approach just been moved to the overall form's load instead of when the menu item is clicked as in the first approach?</p> <p>The second approach definitely seems to perform much better. Any thoughts?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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