简体   繁体   English

WPF Treeview HierarchicalDataTemplate ItemTemplateSelector

[英]WPF Treeview HierarchicalDataTemplate ItemTemplateSelector

I am trying to create a simple 2 level Treeview in WPF (MVVM approach). 我试图在WPF(MVVM方法)中创建一个简单的2级Treeview。 For my first level I have a standard datatemplate, for my second level I want to use a Template Selector so that I can change the appearance of each item based on one of its properties. 对于我的第一个级别,我有一个标准的数据模板,对于我的第二个级别,我想使用模板选择器,以便我可以根据其中一个属性更改每个项目的外观。

Below is my Treeview xaml 下面是我的Treeview xaml

<Treeview ItemsSource={Binding ListA}>
      <TreeView.ItemTemplate>
       <HierarchicalDataTemplate ItemsSource="{Binding ListB}" ItemTemplateSelector={StaticResource TemplateSelector}>
         <Textblock Text={Binding Name}/>
      </HierarchicalDataTemplate>
   </TreeView.ItemTemplate>
</TreeView>

My first level is 我的第一个级别是

<Textblock Text={Binding Name}/> 

will just display a name 只会显示一个名字

For my second level the TemplateSelector is returning a datatemplate which is something like 对于我的第二级,TemplateSelector返回一个类似的数据模板

<DataTemplate x:Key="SomeKey">
<StackPanel Orientation="Horizontal">
<ViewBox>
-----
</ViewBox>
<TextBlock Text={Binding Name}/>
</StackPanel>
</DataTemplate>

But all I see for my second level is my second level ViewModel name. 但我在第二级看到的只是我的第二级ViewModel名称。 I double checked the template selector and it is definitely returning the correct data template but it is just not getting displayed. 我仔细检查了模板选择器,它肯定会返回正确的数据模板,但它只是没有显示。

Can anyone please point me in the right direction? 有谁能指出我正确的方向?

Edit -- Added more code as per request 编辑 - 根据请求添加更多代码

this is my template selector 这是我的模板选择器

public class DataFieldsDataTemplateSelector : DataTemplateSelector
{
public DataTemplate AlphaTemplate { get; set; }
public ------
public ------
public DataFieldsDataTemplateSelector()
{
//This is getting the template from my ResourceDictionary
AlphaTemplate = (DataTemplate)dDictionary["alphaTemplate"];
}
public override DataTemplate SelectTemplate(object item,DependencyObject container)
        {
//Somecode
return AlphaTemplate;
}
}

my template for AlphaTemplate in my dictionary is 我字典中的AlphaTemplate模板是

<DataTemplate x:Key="alphaTemplate">

            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="15"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Viewbox  IsHitTestVisible="False">

                    <Path Data="M0,0L56.622002,0 56.622002,14.471 35.715,14.471 35.715,64 20.715,64 20.715,14.471 0,14.471z" Stretch="Uniform" Fill="{DynamicResource ButtonForegroundNormal}" VerticalAlignment="Center" Width="15" Height="15" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
                        <Path.RenderTransform>
                            <TransformGroup>
                                <TransformGroup.Children>
                                    <RotateTransform Angle="0" />
                                    <ScaleTransform ScaleX="1" ScaleY="1" />
                                </TransformGroup.Children>
                            </TransformGroup>
                        </Path.RenderTransform>
                    </Path>

                </Viewbox>
                <textBlock Text="{Binding Name}/>
            </Grid>
    </DataTemplate>

my class TypeB contains a Name(Text) and DataType(Text) Fields if the DataType is Alpha I return AlphaTemplate in my templateSelector and so on 我的类TypeB包含一个Name(Text)和DataType(Text)字段,如果DataType是Alpha我在templateSelector中返回AlphaTemplate等等

I have an action(dragDrop) on the window which adds items to the second level. 我在窗口上有一个动作(dragDrop),它将项目添加到第二级。 And I want the template selector should pick up the correct datatemplate for that dropped item based on its DataType 我希望模板选择器应该根据其DataType为该删除的项目选择正确的datatemplate

My main ViewModel contains ICollectionView of TypeA Objects and Each TypeA ViewModel contains ICollectionView of TypeB ViewModels. 我的主ViewModel包含TypeA对象的ICollectionView,每个TypeA ViewModel包含TypeB ViewModels的ICollectionView。

Let me know if you need anything 你有任何需要都请告诉我

I dont know what is wrong with this as this will require to debug the code, but what you wanted to achieve can be done by defining the default DataTemplate for your TypeB and switching the content depending on the binding like this: 我不知道这有什么问题,因为这需要调试代码,但你想要实现的是通过为TypeB定义默认的DataTemplate并根据绑定切换内容,如下所示:

<DataTemplate DataType="{x:Type TypeB}">
    <ContentControl>
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <!-- Default template here for your item -->
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding XYZ}" Value="true">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <!-- Different template for your item -->
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>        
</DataTemplate>

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM