WPF TreeviewItem parent selected event fired after selecting a child?

c#, events, treeview, wpf

Solution

This has to be something with your code. I'm using a TreeView right now and child selection doesn't trigger parent selection for me. Can you post more of your code? Perhaps I can spot the issue.

I stand corrected. After looking through my code a bit more and running some tests it appears this is normal behavior for the control. If you don't want selections to bubble up then yes you should set the `Handled` value to `true` of the `RoutedEventArgs` parameter.

EDIT:

Here's the XAML I used:

<TreeView Width="200" Height="300">
    <TreeViewItem Header="Parent" Selected="Parent_Selected">
        <TreeViewItem Header="Child" Selected="Child_Selected" />
    </TreeViewItem>
    <TreeViewItem Header="Parent" Selected="Parent_Selected">
        <TreeViewItem Header="Child" Selected="Child_Selected" />
    </TreeViewItem>
</TreeView>

And the code:

private void Parent_Selected(object sender, RoutedEventArgs e)
{
    bool test = false;
}

private void Child_Selected(object sender, RoutedEventArgs e)
{
    bool test = false;
    // e.Handled = true;
}

If the `e.Handled = true` statement is left commented then `Parent_Selected` will fire when the child object is selected.

Problem

I have a standard WPF treeview item with a number of children. When creating the tree programatically I add selected event handlers to the parent and children items. What I am finding is that when after the child items selected event has fired and being handled, the parents event is then fired. The problem with this is that I populate a datagrid based on the selected item. So everytime after selecting a child item the datagrid is reset to the parent item selection. Please could someone explain if this item is normal or if I'd doing something wrong and how to fix it. Please let me know if you require anymore information. Hooking up event handlers: ``` //parent TreeViewItem tvi = new TreeViewItem(); tvi.Header = str; tvi.Selected += CoreSupplierSelected //child TreeViewItem tvi = new TreeViewItem(); tvi.Header = str; tvi.Tag = resinSystems.Values[i]; tvi.Selected += CoreResinSystemSelected; ``` Handlers: ``` private void CoreSupplierSelected(object sender, RoutedEventArgs e) { TreeViewItem item = e.OriginalSource as TreeViewItem; MaterialSelectionData thicknessData = editInterface.GetCoreThicknessData(new List<object>() { item.Tag }, null); List<string> columnNames = thicknessData.DisplayFieldTitles; columnNames.Insert(0,""); DataTable dt = GUICommon.DatableConverter.ToDataTable(thicknessData.DisplayData, columnNames); dtgCores.ItemsSource = dt.AsDataView(); dtgCores.Columns[0].Visibility = System.Windows.Visibility.Collapsed; } private void CoreResinSystemSelected(object sender, RoutedEventArgs e) { TreeViewItem item = e.OriginalSource as TreeViewItem; GX3MaterialSelectionData thicknessData = editInterface.GetCoreThicknessData(new List<object>() { ((TreeViewItem)item.Parent).Tag }, new List<object>() { item.Tag }); List<string> columnNames = thicknessData.DisplayFieldTitles; columnNames.Insert(0, ""); DataTable dt = GUICommon.DatableConverter.ToDataTable(thicknessData.DisplayData, columnNames); dtgCores.ItemsSource = dt.AsDataView(); e.Handled = true; dtgCores.Columns[0].Visibility = System.Windows.Visibility.Collapsed; } ```

Original source