2013-07-16 55 views
0

或者,如果我需要這種功能,我是唯一選擇擴展原始樹視圖的選項嗎?有沒有辦法在默認的treeview中定義多個itemtemplate?

我想是這樣的,但有限定,使得它們都使用相同的項目,但應用不同的模板來創建這兩個點控制兩個不同的模板兩個itemspresenters。

 <ControlTemplate> 
      <Grid> 
      <wpfExp:SignalNameBox x:Name="TreeViewTimeTextBox" Grid.Row="0" Grid.Column="0" 
        Height="{Binding RelativeSource = {RelativeSource AncestorType={x:Type wpfExp:GraphViewer}}, Path=GraphHeight}" 
        Width="200" 
        Margin="19,0,0,0" 
        MainText="Time" 
       /> 
      <wpfExp:SignalGraphAxis 
       x:Name="signal_axis" 
       VerticalAlignment="Stretch" 
       HorizontalAlignment="Stretch" 
       GraphHeight="{Binding RelativeSource = {RelativeSource AncestorType={x:Type wpfExp:GraphViewer}}, Path=GraphHeight}" 
       MinWidth="10" 
       MinHeight="10" 
       PenColor="{Binding ElementName=AxisColorPicker, Path=SelectedColor, Mode=OneWay}" 
       PenWidth="{Binding RelativeSource = {RelativeSource AncestorType={x:Type wpfExp:GraphViewer}}, Path=GraphPenWidth, Mode=OneWay}" 
       MaxTimeValue="{Binding RelativeSource = {RelativeSource AncestorType={x:Type wpfExp:GraphViewer}}, Path=_SignalDataViewModel.MaxTimeValue}" 
       TimeUnit="{Binding RelativeSource = {RelativeSource AncestorType={x:Type wpfExp:GraphViewer}}, Path = TimeUnit}" 
       AxisDivisionUnit="{Binding RelativeSource = {RelativeSource AncestorType={x:Type wpfExp:GraphViewer}}, Path = AxisDivisionUnit}" 
       /> 
      <StackPanel> 
       <ItemsPresenter/> 
      </StackPanel> 
      <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> 
       <StackPanel> 
       <ItemsPresenter/> 
       </StackPanel> 
      </ScrollViewer> 
      </Grid> 
     </ControlTemplate> 
+0

是的,請ItemTemplateSelector使用它。 http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplateselector.aspx – Nitesh

回答

1

我在這裏採取的一個最簡單的例子是根據他們的性別(男性/女性)顯示具有不同背景顏色的聯繫人。

所以這是ItemTemplateSelector類將如何看待

class MyDataTemplateSelector : DataTemplateSelector 
{ 
    public override DataTemplate SelectTemplate(object item, DependencyObject container) 
    { 
     if (item != null) 
     {     
      Contact objContact = item as Contact ; 

      switch (objContact.Sex) 
      { 
       case "Male": return App.Current.MainWindow.FindResource("TemplateMale") as DataTemplate; 
       case "Female": return App.Current.MainWindow.FindResource("TemplateFemale") as DataTemplate; 
      } 
     } 

     return null; 
    } 
} 

,這是你怎麼能在你的XAML

<Window.Resources>  
    <local:MyDataTemplateSelector x:Key="myTemplateSelector"></local:MyDataTemplateSelector> 

    <DataTemplate x:Key="TemplateMale"> 
     <TextBlock Background="Blue" Text="{Binding Name}"></TextBlock> 
    </DataTemplate> 
    <DataTemplate x:Key="TemplateFemale"> 
     <TextBlock Background="Pink" Text="{Binding Name}"></TextBlock> 
    </DataTemplate> 
</Window.Resources> 

<TreeView ItemsSource="{Binding Contacts}"    
      ItemTemplateSelector="{StaticResource myTemplateSelector}"> 
</TreeView> 
相關問題