<TreeView Name="MyTreeView" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling">
<TreeView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</TreeView.ItemsPanel>
<TreeView.Resources>
<DataTemplate DataType="{x:Type EntityType:MyFixedDevice}">
<TreeViewItem IsHitTestVisible="True" IsEnabled="True">
<TreeViewItem.Header>
<TextBlock Text="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource NameConverter}}"
IsHitTestVisible="True" IsEnabled="True"/>
</TreeViewItem.Header>
</TreeViewItem>
</DataTemplate>
</TreeView.Resources>
<TreeView.Items>
<TreeViewItem Header="Data Warehouse">
<TreeViewItem.Items>
<TreeViewItem Header="Platforms">
<TreeViewItem.Items>
<TreeViewItem ItemsSource="{Binding OBJS, Converter={StaticResource COBJSourceConverter}, ConverterParameter=Fixed}">
<TreeViewItem.Header>
<TextBlock Text="{Binding RelativeSource={RelativeSource Self},
Path=Parent.Items.Count,
StringFormat=Fixed Devices ({0})}">
</TextBlock>
</TreeViewItem.Header>
</TreeViewItem>
</TreeViewItem.Items>
</TreeViewItem>
</TreeViewItem.Items>
</TreeViewItem>
</TreeView.Items>
</TreeView>
How come left-click on the TreeViewItems that are created with DataTemplate does not select them? How come if I select them in code, I can't select them again or deselect them?
TreeViewItem selectedItem = MyTreeView.SelectedItem as TreeViewItem;
if(selectedItem != null) {
selectedItem.IsSelected = false;
MyTreeView.Focus();
}
I've tried to use the below to unselect TreeViewItems in the TreeView, but it only deselects the TreeViewItems if they are statically set in the XAML, and not if they are created using an ItemsSource and DataTemplate?