有没有办法在默认树视图中定义多个itemtemplate?

时间:2013-07-16 00:24:04

标签: wpf xaml treeview

或者,如果我想要这个功能,我是扩展原始树视图的唯一选择吗?

我想要这样的东西,但是两个itempresenters定义了两个不同的模板,这样他们都使用相同的项目,但应用不同的模板来创建这两个点的控件。

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

1 个答案:

答案 0 :(得分: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>