Note that there are some explanatory texts on larger screens.

plurals
  1. POTabControl ItemTemplate without ItemsSource
    primarykey
    data
    text
    <p>I am doing a WPF application with a TabControl. At the beginning I had a TabControl bound to ObservableCollection of TabBase items, where TabBase is a base class for tab viewmodel:</p> <pre><code>&lt;TabControl IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Tabs}" ItemTemplate="{StaticResource ClosableTabTemplate}" ... public ObservableCollection&lt;TabBase&gt; Tabs { get; private set; } ... public abstract class TabBase : ViewModelBase ... public abstract class ViewModelBase : INotifyPropertyChanged { public virtual string DisplayName { get; protected set; } ... &lt;DataTemplate x:Key="ClosableTabTemplate"&gt; &lt;DockPanel Width="120"&gt; &lt;Button Command="{Binding Path=CmdClose}" Content="X" /&gt; &lt;ContentPresenter Content="{Binding Path=DisplayName}"&gt; &lt;/ContentPresenter&gt; &lt;/DockPanel&gt; &lt;/DataTemplate&gt; </code></pre> <p>But I've faced with an issue when I switch tabs it looks like current tab is being created each time, even if it was already opened before. Searching thru StackOverflow I've found the solution <a href="https://stackoverflow.com/questions/1389769/wpf-virtualizingstackpanel-for-increased-performance">here</a> with reference to <a href="http://groups.google.com/group/wpf-disciples/browse_thread/thread/6f3531a1720252dd" rel="nofollow noreferrer">here</a>. I've replaced using of declarative ItemsSource with dynamic creation of tabs from code. Tabs switching performance issue was resolved, but tab headers have lost link to template, so instead of tab header with caption and close button I see just a little tab header without anything. Playing a bit with tab creation code, I was able to restore tab size and close button, but without binding - there is no caption and close button doesn't work (5 lines with item.Header restored original tab size):</p> <pre><code> private void AddTabItem(TabBase view) { TabItem item = new TabItem(); item.DataContext = view; item.Content = new ContentControl(); (item.Content as ContentControl).Focusable = false; (item.Content as ContentControl).SetBinding(ContentControl.ContentProperty, new Binding()); item.Header = new ContentControl(); (item.Header as ContentControl).DataContext = view; (item.Header as ContentControl).Focusable = false; (item.Header as ContentControl).SetBinding(ContentControl.ContentProperty, new Binding()); item.HeaderTemplate = (DataTemplate)FindResource("ClosableTabTemplate"); tabControl.Items.Add(item); } </code></pre> <p>The question is, how can I make ItemTemplate working for TabControl without ItemsSource binding?</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