0

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

我想要这样的东西,但是两个 itempresenter 定义了两个不同的模板,以便它们都使用相同的项目,但应用不同的模板在这两个位置创建控件。

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

1 回答 1

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>
于 2013-07-16T03:27:21.957 回答