开发者

How to change HirerachicalDataTemplate associated with the WPF TreeView programatically at run time?

开发者 https://www.devze.com 2023-04-05 16:32 出处:网络
I have a TreeView control on my WPF window. I am giving only relevant XAML from my window. <Window.Resources>

I have a TreeView control on my WPF window. I am giving only relevant XAML from my window.

<Window.Resources>
        <HierarchicalDataTemplate x:Key="HierarchicalTemplate" ItemsSource="{Binding SubOrgUnitItems}">
            <StackPanel Orientation="Horizontal">
                <Image Height="16" Source="{Binding ImagePath}" Stretch="Fill" Width="16"/>
                <TextBlock Text="{Binding OrgUnitName}" Name="treeText" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </Window.Resources>



<TreeView Margin="10,35,10,10" BorderThickness="1" VerticalAlignment="Stretch" HorizontalAlignment="Stret开发者_运维知识库ch" ScrollViewer.VerticalScrollBarVisibility="Auto"
                      IsTabStop="True" Name="orgTreeView" ItemsSource="{Binding}"  ItemTemplate="{DynamicResource HierarchicalTemplate}" TabIndex="700" SelectedItemChanged="orgTreeView_SelectedItemChanged" />

When the Collection of Organisations is bound to DataContext of the TreeView, items are displayed with the OrgUnitName's value as a text at every node. Now at run time I want to see some other property's value as a text at every node. e.g. OrgUnitCode instead of OrgUnitName. Both are properties declared in the view model class associated with the treeview.

How can i do it programatically at run time?


You should use HierarchicalDataTemplateSelector.

Define two different HierarchicalDataTemplate (as you did).

Inherite your custom selector class from DataTemplateSelector, override its SelectTemplate method and put there the logic of the selection. This method will return the correct template in each case.

Create a Resource(Custom Selector class) in the xaml file.

Set the TreeViews ItemTemplateSelector to the static selector resource.

See a simple example here: Link


I have achieved what I wanted to do, but unfortunately by some work around. Following thing worked for me but it may not be the correct answer to the problem.

I added one more HirerachicalDataTemplate and TreeView. The new template uses the OrgUnitCode property. The new tree view uses the new template.

<HierarchicalDataTemplate x:Key="HierarchicalTemplateUsingCode" ItemsSource="{Binding SubOrgUnitItems}">
            <StackPanel Orientation="Horizontal">
                <Image Height="16" Source="{Binding ImagePath}" Stretch="Fill" Width="16"/>
                <TextBlock Text="{Binding OrgUnitCode}" Name="treeText" />
            </StackPanel>
        </HierarchicalDataTemplate>



<TreeView Margin="10,35,10,10" BorderThickness="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Auto"
                      IsTabStop="True" Name="orgTreeViewCode" ItemsSource="{Binding}"  ItemTemplate="{DynamicResource HierarchicalTemplateUsingCode}" TabIndex="700" SelectedItemChanged="orgTreeViewCode_SelectedItemChanged"  Visibility="Hidden"/>

At run time, when I want to see OrgUnitCode property value as a text at the node, I simply make new tree visible and hide the first one (mentioned in question). So making tree views visible/invisible help me to achieve what I wanted to do.

0

精彩评论

暂无评论...
验证码 换一张
取 消