3

私は、ItemsSource が、それぞれのルート要素として TabItem を持つ監視可能なビューのコレクション (UserControls) にバインドされている TabControl持っています。ただし、表示されると、UserControlラッパーが競合を引き起こしているかのように、ヘッダーテキストは各 TabItem のコンテンツにあります。

代替テキスト

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

TabItems が TabControl 内の TabItems として表示されるようにするには、何を変更する必要がありますか?

SmartFormAreaView.xamlと呼ばれる TabItem ビューを次に示します。

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

2 に答える 2

4

ItemsControl の場合、Items コレクションに (直接または 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 は、適切なプレゼンターに自動的に設定されます。

さまざまな ItemsControl 関連トピックの優れた議論については、WPF 博士のブログを参照してください。

于 2009-08-04T10:47:20.047 に答える
0

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

したがって、通常TabItemsのビジュアルツリーを埋めるか、サブクラスTabItems化するか、サブクラス化しTabControlてそのメソッドをオーバーライドしIsItemItsOwnContainerOverride、タイプをコンテナとして受け入れます。

メソッドは次のようになります。

protected override bool IsItemItsOwnContainerOverride(object item)
{
    return item is YourControlTypeHere || item is TabItem;
}
于 2009-08-04T10:12:36.323 に答える