Why do tab headers appear in the contents area of ​​tabs in the XAML TabControl?

I have a TabControl whose ItemsSource is bound to an observable collection of views (UserControls), each of which has a TabItem in its root element . However, when it is displayed, the Header text is in the contents of each TabItem element, since the UserControl shell causes conflicts:

alt text http://i31.tinypic.com/2z7pctz.png

TabControl is located in SmartFormView.xaml:

<UserControl x:Class="TestApp.Views.SmartFormView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel
        Margin="10">
        <TextBlock Text="{Binding Title}"
            FontSize="18"/>
        <TextBlock Text="{Binding Description}"
            FontSize="12"/>

        <TabControl
            Margin="0 10 0 0"
            ItemsSource="{Binding SmartFormAreaViews}"/>
    </StackPanel>
</UserControl>

What do I need to change for TabItems to appear as TabItems inside TabControl?

Here are the TabItem views called SmartFormAreaView.xaml:

<UserControl x:Class="TestApp.Views.SmartFormAreaView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TabItem Header="This is the header">
        <StackPanel Margin="10">
            <TextBlock Text="this is the content"/>
        </StackPanel>
    </TabItem>
</UserControl>

ObservableCollection:

var areas = from area in xmlDoc.Descendants("area")
            select area;
foreach (var area in areas)
{
    SmartFormArea smartFormArea = new SmartFormArea();
    smartFormArea.IdCode = area.Attribute("idCode").Value;
    smartFormArea.Title = area.Attribute("title").Value;
    SmartFormAreaPresenter smartFormAreaPresenter = new SmartFormAreaPresenter(smartFormArea);
    SmartFormAreaViews.Add(smartFormAreaPresenter.View as SmartFormAreaView);
}
+3
2

ItemsControl, , Items ( ItemsSource), , . item - , TabItem ListBoxItem. ContentControl HeaderedContentControl, Content, .., , . , ItemConttainStyle ItemControl.

ItemsSource SmartFormAreaPresenters. :

<TabControl ItemsSource="{Binding SmartFormAreaPresenters}">
  <TabControl.ItemContainerStyle>
    <Style TargetType="{x:Type TabItem}">
      <Setter Property="Header" Value="{Binding HeaderText}" />
    </Style>
  </TabControl.ItemContainerStyle>

  <TabControl.ContentTemplate>
    <DataTemplate DataType="{x:Type local:SmartFormAreaPresenter}">
      <local:SmartFormAreaView />
    </DataTemplate>
  </TabControl.ContentTemplate>
</TabControl>

HeaderText SmartFormAreaPresenter. TabItem SmartFormAreaView. DataContext .

. Dr. WPF blog , ItemsControl.

+4

TabControl , TabItem, UserControl SmartFormAreaView ..

, TabItems , TabItems, TabControl, IsItemItsOwnContainerOverride, .

:

protected override bool IsItemItsOwnContainerOverride(object item)
{
    return item is YourControlTypeHere || item is TabItem;
}
0

Source: https://habr.com/ru/post/1714289/


All Articles