2014-05-20 18 views
6

Binada, TabControl numaralı bir ItemsSource aracılığıyla çeşitli sekmeler görüntülemek istediğim bir projem var. Ayrıca, TabControl'un başlangıcında, ItemsSource'da bulunamayan birkaç "genel bakış" sekmesine sahip olmam gerekiyor.ItemsSource kullanırken daha fazla öğe ekleyin

Bunu başarmanın en iyi yolu nedir, düşünebilmemin tek yolu benim XAML'mdeki genel bakış sekmelerimin olması ve ItemSource kullanmak yerine kod aracılığıyla el ile sekme öğeleri eklemektir. Bu en iyi yoldur. o.

cevap

8

Sen CompositeCollection (MSDN) Bunu gerçekleştirmek için kullanabilirsiniz:

<Window.Resources> 
    <CollectionViewSource x:Key="ExistingTabs" Source="{Binding ExistingTabs}"/> 
</Window.Resources> 
<TabControl> 
    <TabControl.ItemsSource> 
     <CompositeCollection> 
      <TabItem>SpecialItem</TabItem> 
      <CollectionContainer Collection="{Binding Source={StaticResource ExistingTabs}}"/> 
     </CompositeCollection> 
    </TabControl.ItemsSource> 
</TabControl> 
-2

Maalesef, ItemsSource bağlantısını açıkça eklenen öğeler koleksiyonu nesneleriyle karıştıramazsınız. Bu nedenle iki seçeneğiniz var, sabit öğeleri ve ardından öğeler listenizdeki öğeleri Öğeler koleksiyonuna elle ekleyin veya ItemsSource'u hem bir dizi sabit nesneyi hem de ciltlenmiş koleksiyonunuzun öğeleri içeren bir koleksiyona bağlayın. Her iki durumda da, en büyük sorun muhtemelen verileriniz değiştiğinde güncelleniyor. Doğru öğelerin kaldırıldığından/eklendiğinden ve UI'nin doğru şekilde güncellendiğinden emin olun.

+1

-1 Bu doğru değil. DataBound CollectionSources artı rasgele XAML tanımlı öğeleri eklemek için kullanılabilecek bir "CompositeCollection" var. –

+0

Bunu bir cevap olarak sunmak ister misiniz? – Hugoagogo

0

Sen CompositeCollection kullanabilirsiniz arkasında How do you add a generic item to a ComboBox bound to a collection in WPF

<TabControl> 
     <TabControl.ItemsSource> 
      <CompositeCollection> 
       <TabItem Header="extra tab item"> //Not bound 
        <TextBox>something</TextBox> 
       </TabItem> 
       <CollectionContainer x:Name="cc"/> 
      </CompositeCollection> 
     </TabControl.ItemsSource> 
    </TabControl> 

Kodu: herkes için

cc.Collection=yourObservableCollection 
2

yol bulmak usi CollectionContainer ile ng HeaderTemplate/ContentTemplate:

İlk ViewModel Type özelliği

<Window x:Class="TabDemo.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:TabDemo" 
     xmlns:vm="clr-namespace:TabDemo.ViewModel" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525" 
     d:DataContext="{d:DesignInstance vm:TabViewModel}"> 
    <Window.Resources> 
     <CollectionViewSource x:Key="ExistingTabs" Source="{Binding ExistingTabs}"/> 
     <DataTemplate x:Key="TemplateForTheHeader" DataType="{x:Type vm:TabViewModel}"> 
      <TextBlock Text="{Binding Title}"/> 
     </DataTemplate> 
     <DataTemplate x:Key="TemplateForTheContent" DataType="{x:Type vm:TabViewModel}"> 
      <DockPanel> 
       <DataGrid ItemsSource="{Binding Data}"></DataGrid> 
      </DockPanel> 
     </DataTemplate> 
     <Style x:Key="TabItemStyle" TargetType="TabItem"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=Type}" Value="{x:Type vm:TabViewModel}"> 
        <Setter Property="HeaderTemplate" Value="{StaticResource TemplateForTheHeader}" /> 
        <Setter Property="ContentTemplate" Value="{StaticResource TemplateForTheContent}" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <TabControl Grid.Row="1" ItemContainerStyle="{StaticResource TabItemStyle}"> 
      <TabControl.ItemsSource> 
       <CompositeCollection> 
        <TabItem Header="Fixed Header"> 
         <TabItem.Content> 
          <TextBlock Text="Fixed Content"/> 
         </TabItem.Content> 
        </TabItem> 
        <CollectionContainer Collection="{Binding Source={StaticResource ExistingTabs}}"/> 
       </CompositeCollection> 
      </TabControl.ItemsSource> 
     </TabControl> 
    </Grid> 
</Window> 
tarafından tanımlanan dinamik sekmeler için HeaderTemplate/ContentTemplate ayarlamak için

public Type Type { get { return this.GetType(); } } 

Kullanım Style.Triggers Tipi Özellik Ekle

Başvuru Anderson Imes 'nun yanıtı: https://stackoverflow.com/a/1348369/1196637

İlgili konular