MaterialDesignInXamlToolkit
MaterialDesignInXamlToolkit copied to clipboard
MaterialDesignTheme breaks [ItemsControl].ItemContainerGenerator.ContainerFromItem
I'm adding an item containing a sub-item in a TreeView, then trying to expand the item and select the sub-item
This is the item container:
ObservableCollection<ModelA> items = new ObservableCollection<ModelA>();
This code runs at startup:
InitializeComponent();
ModelA mA = new ModelA() { Name = "Model A" };
ModelB mB = new ModelB() { Name = "Model B" };
mA.Children.Add(mB);
items.Add(mA);
tree.ItemsSource = items;
I select the non-expanded Model A manually, then i run this code:
ModelB newModelB = new ModelB { Name = "New Model B" };
ModelA selectedModelA = ((ModelA)tree.SelectedItem);
selectedModelA.Children.Add(newModelB);
TreeViewItem tvi = tree.ItemContainerGenerator.ContainerFromItem(selectedModelA) as TreeViewItem;
tvi.IsExpanded = true;
tree.UpdateLayout();
TreeViewItem tvi2 = tvi.ItemContainerGenerator.ContainerFromItem(newModelB) as TreeViewItem;
tvi2.IsSelected = true;
ModelA contains property "Children" of type ObservableCollection< ModelB > and property "Name" of type string ModelB property "Name" of type string
On the XAML side, the TreeView resources contain a "HierarchicalDataTemplate" of DataType ModelA and a "DataTemplate" of DataType ModelB, i can post the full XAML in a future post if necessary
This code runs fine when the MaterialDesingTheme is not enabled in App.xaml, and breaks when enabled
When MaterialDesignTheme is disabled, ModelA is expanded and the new ModelB is selected successfully.
When MaterialDesignTheme is enabled, tvi2 remains null and tvi2.IsSelected = true; can't run
Edit: after further investigation, style MaterialDesignTreeViewItem in "MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.TreeView.xaml" is what's breaking the function, from my understanding some UIElement is in the way between the TreeViewItem and my Item, is this correct?
Can you provide a test repo please ?