Changing the DataTemplate of a TreeView in runtime

牧云@^-^@ 提交于 2019-12-02 10:23:16

Thanks to the helfupful comment from @Bob, I solved the problem like this:

I defined a property in the UserControl, where I have the TreeView.

I changed the HierarchicalDataTemplate of level 1 like this:

<HierarchicalDataTemplate x:Key="Level1Template" 
                      ItemsSource="{Binding Value}"
                      ItemTemplateSelector="{StaticResource MySelector}">
    <Border Background="Green">
        <TextBlock Text="Level1"/>
    </Border>
</HierarchicalDataTemplate>

I implemented the MySelector class and implemented the Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate function. Using the container, I could access my UserControl and its properties.

The ItemsSource of my TreeView changes when I press a button in my usercontrol. After each change, the TemplateSelector is called, and so the template is updated.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!