Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't believe you may avoid this behavior, unless you use another control other than the TabControl.</p> <p>The TabControl is a ItemControl-derived component: if its content is created via DataTemplate (as you done), the actual content is generated every time upon the current selection.</p> <p>Another option is to fill the TabControl with direct content, and the inner controls should be preserved across the selection.</p> <p>Have a look here: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.tabcontrol(v=vs.110).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.windows.controls.tabcontrol(v=vs.110).aspx</a></p> <p>UPDATE: First off, I am not sure to have understand what you want, but here is a solution based on what I mean.</p> <p>The fundamental trick is hosting directly the tab-contents, instead of creating via data-templating. That is:</p> <pre><code> &lt;TabControl&gt; &lt;TabItem Header="Tab1"&gt; &lt;TextBlock Text="Page 1" HorizontalAlignment="Center" VerticalAlignment="Center" /&gt; &lt;/TabItem&gt; &lt;TabItem Header="Tab2"&gt; &lt;CheckBox Content="check me" HorizontalAlignment="Center" VerticalAlignment="Center" /&gt; &lt;/TabItem&gt; &lt;TabItem Header="Tab3"&gt; &lt;TextBlock Text="Page 3" HorizontalAlignment="Center" VerticalAlignment="Center" /&gt; &lt;/TabItem&gt; &lt;/TabControl&gt; </code></pre> <p>The above snippet should "persist" the checkbox value across the tabs flipping.</p> <p>But you need (or desire) some kind of templating, or a dynamic way to create the right content upon a certain data added to the tabcontrol.</p> <p>The following trick should solve your problem:</p> <pre><code> &lt;TabControl&gt; &lt;TabItem Header="Tab1" &gt; &lt;ContentControl Content="{Binding Path=A}" ContentTemplateSelector="{StaticResource sel}" /&gt; &lt;/TabItem&gt; &lt;TabItem Header="Tab2" &gt; &lt;ContentControl Content="{Binding Path=B}" ContentTemplateSelector="{StaticResource sel}" /&gt; &lt;/TabItem&gt; &lt;TabItem Header="Tab3" &gt; &lt;ContentControl Content="{Binding Path=C}" ContentTemplateSelector="{StaticResource sel}" /&gt; &lt;/TabItem&gt; &lt;/TabControl&gt; </code></pre> <p>This is using a trivial set of templates as follows:</p> <pre><code>&lt;Window.Resources&gt; &lt;local:MySelector x:Key="sel" /&gt; &lt;DataTemplate x:Key="dtplA"&gt; &lt;StackPanel Margin="30,30"&gt; &lt;TextBlock Text="A: " /&gt; &lt;TextBox /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="dtplB"&gt; &lt;StackPanel Margin="30,30"&gt; &lt;TextBlock Text="B: " /&gt; &lt;TextBox /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="dtplC"&gt; &lt;StackPanel Margin="30,30"&gt; &lt;TextBlock Text="C: " /&gt; &lt;TextBox /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; </code></pre> <p>And the behind-code is even more trivial:</p> <pre><code>public class VM { public A A { get; set; } public B B { get; set; } public C C { get; set; } } public class A { } public class B { } public class C { } public class MySelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { if (item != null) { return (DataTemplate)((FrameworkElement)container).FindResource("dtpl" + item.GetType().Name); } else { return base.SelectTemplate(item, container); } } } </code></pre> <p>If you try this small example, the text typed into the textboxes will persist across the tabs flipping. That is, the templates will be called once only.</p> <p>Let me know.</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.
    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