2009-08-04 2 views
3

私は、そのビューのItemsSourceそのルート要素のTabItemとして持つ(ユーザーコントロール)各観察可能なコレクションにバインドされているTabControlのを持っています。XAML TabControlのタブのコンテンツ領域にタブヘッダーが表示されるのはなぜですか?

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

タブコントロールがSmartFormView.xamlである:

それが表示されるときユーザーコントロールラッパーが競合を引き起こしているかのようしかし、ヘッダテキストは、それぞれのTabItemのコンテンツであります
<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> 

TabControlの内部にTabItemが表示されるように変更する必要があるのは何ですか?ここで

SmartFormAreaView.xamlと呼ばれるのTabItem図である:私は作成してのObservableCollectionに各ビューをロードどこ

<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> 

そして、ここでは、次のとおりです。

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); 
} 

答えて

4

を、アイテムがそのアイテムのコレクションに追加した場合(直接またはのItemsSourceを介して)、そのコントロールのアイテムコンテナのインスタンスではなく、各項目でありますアイテムコンテナのインスタンスにラップされます。項目コンテナは、TabItemやListBoxItemなどのクラスです。アイテムコンテナは通常ContentControlまたはHeaderedContentControlであり、実際のアイテムはContentプロパティに割り当てられているため、テンプレートなどを使用してコンテンツの表示方法を制御できます。 ItemControlのItemContainerStyleプロパティを使用してアイテムコンテナ自体のスタイルを設定することもできます。

この場合、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上の適切なプロパティです。また、SmartFormAreaView定義からTabItemを削除する必要があります。各ビューのDataContextは自動的に適切なPresenterに設定されます。

さまざまなItemsControl関連トピックの優れた説明については、Dr. WPFのblogを参照してください。

0

TabControlますUserControl、SmartFormAreaViewなどではなく、TabItemにキャストできる場合にのみ、コントロールとしてコントロールを受け入れます。

つまり、あなたのビジュアルツリーとの定期的なTabItemsを埋めるのいずれか、またはあなたがTabItemsをサブクラス化するか、コンテナとしてあなたのタイプを受け入れるために、そのIsItemItsOwnContainerOverrideメソッドをオーバーライドするTabControlをサブクラス化。

次のようにメソッドが見えるべきである:任意のItemsControlについて

protected override bool IsItemItsOwnContainerOverride(object item) 
{ 
    return item is YourControlTypeHere || item is TabItem; 
} 
関連する問題