How to trigger the Listview behind when Treeview is click?
NickName:rayna qarla Ask DateTime:2019-05-06T10:11:21

How to trigger the Listview behind when Treeview is click?

I have treeview inside my Listview like below

 <ListView  x:Name="LvItemDisplay" ItemsSource="{Binding itemDisplayList}" SelectionChanged="LvItemDisplay_SelectionChanged">

      <ListView.ItemTemplate>

            <DataTemplate x:Name="dtItemDisplay">

                    <TreeView x:Name="TvItemDisplay" Background="Transparent" ItemsSource="{Binding itemDisplayList}" MouseDown="TvItemDisplay_MouseDown_1" SelectedItemChanged="TvItemDisplay_SelectedItemChanged"  >
                        .
                        .
                        .

I have click event for both treeview and listview. both of it when click it will popup a dialog box that user can edit that will binding from them.

the problem is, whenever user want to click the treeview user need to click on the listview first to get the SelectedItem. if not nothing will happen since the SelectedItem will be null.

this is the code behind for listview

private void LvItemDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

                ListViewItem listViewItem = (ListViewItem)(LvItemDisplay.ItemContainerGenerator.ContainerFromItem(LvItemDisplay.SelectedItem));
                TreeView treeView = null;
                DialogHost dialogHost = null;

                if (LvItemDisplay.SelectedIndex != -1)
                {
                    if (listViewItem != null)
                    {
                        //get the listViewItem's template parent
                        ContentPresenter templateParent = GetFrameworkElementByName<ContentPresenter>(listViewItem);

                        //get the DataTemplate that treeview in.
                        DataTemplate dataTemplate = LvItemDisplay.ItemTemplate;

                        if (dataTemplate != null && templateParent != null)
                        {

                            treeView = dataTemplate.FindName("TvItemDisplay", templateParent) as TreeView;
                            if (treeView != null)
                            {
                                dialogHost = dataTemplate.FindName("dialogBoxEditQty", templateParent) as DialogHost;
                                dialogHost.IsOpen = true;
                            }

                        }

                    }
                }
        }

and for treeview

  private void TvItemDisplay_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        ListViewItem listViewItem = (ListViewItem)(LvItemDisplay.ItemContainerGenerator.ContainerFromItem(LvItemDisplay.SelectedItem));
        TreeView treeView = null;
        DialogHost dialogHost = null;

            if (listViewItem != null)
            {
               //get the listViewItem's template parent
                ContentPresenter templateParent = GetFrameworkElementByName<ContentPresenter>(listViewItem);

                //get the DataTemplate that treeview in.
                DataTemplate dataTemplate = LvItemDisplay.ItemTemplate;

                if (dataTemplate != null && templateParent != null)
                {
                    treeView = dataTemplate.FindName("TvItemDisplay", templateParent) as TreeView;
                }

                if (treeView != null)
                {
                    dialogHost = dataTemplate.FindName("dialogBoxEditQty", templateParent) as DialogHost;
                    dialogHost.IsOpen = true;
                }               
        }          
    }

save for dialogHost

private void ButtonSave_Click(object sender, RoutedEventArgs e)
    {

        try
        {

            // get the current selected item
            ListViewItem listViewItem = (ListViewItem)(LvItemDisplay.ItemContainerGenerator.ContainerFromItem(LvItemDisplay.SelectedItem));


            DialogHost dialogHost = null;
            if (listViewItem != null)
            {
            .
            .
            .

I want something like whenever user click on the treeview it will triggered the listview behind /click the listview as well. is it possible ?

Copyright Notice:Content Author:「rayna qarla」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/55998011/how-to-trigger-the-listview-behind-when-treeview-is-click

More about “How to trigger the Listview behind when Treeview is click?” related questions

How to trigger the Listview behind when Treeview is click?

I have treeview inside my Listview like below &lt;ListView x:Name="LvItemDisplay" ItemsSource="{Binding itemDisplayList}" SelectionChanged="LvItemDisplay_SelectionChanged"&gt; &lt;ListVie

Show Detail

How to create datatemplate in code behind with listview gridview for a treeview

Hi Please help me to realize the fallowing datatemplate in code behind &lt;TreeView Name="OrderTree" DataContext="{Binding ordersTree}" ItemsSource="{Binding}"&gt; &lt;TreeView.Resources&gt;&#

Show Detail

TreeView & ListView WPF

suppose i have bind my treeview and plus sign is coming and i want when i will click on the plus sign then dynamically a listview or grid will show just under the node where i will click and relate...

Show Detail

How to I populate a treeView from a mouse click on an item in listView in C#?

How to I populate a treeView from a mouse click on an item in listView in C#? Data in the listView is read from an access database.

Show Detail

How to trigger TreeView LostFocus event

I have a TreeView like this: &lt;TreeView LostFocus="treeView_LostFocus"&gt; &lt;TreeViewItem Selected="treeViewItem_select"/&gt; &lt;/TreeView&gt; it always

Show Detail

How can i add context menu for each item on listView1 and treeView1 when doing mouse right click on item?

The items in the listView1 are files so i want that the menu for each file when i will do on the file right click with the mouse will show different options(properties) then if i'm doing right clik...

Show Detail

How to update treeview when used edits listview/Gridview bound to same data?

I have a set of collections based on a class called "Foo", and the class includes a property "Bar", along with a fairly long list of other values to be displayed and edited. I am using two semi-

Show Detail

Use ListView and TreeView to list folders and files

I have a windows form that contains two controls (TreeView and ListView), i want to display all directories for a specific path in the TreeView and when the user clicks to any directory listed in the

Show Detail

Trigger TreeView AfterSelect event multiple times

I have a TreeView control. Say I have 5 nodes in it. On selecting a node, I populate a ListView with all the Directories under that SelectedNode. Then, I click a Button, which modifies the ListView...

Show Detail

Select node in treeView on right click MVVM

I want to select a node of tree view on right click. I am using MVVM pattern and don't want to achieve this in code behind. Here is my XAML for tree view. &lt;TreeView Margin="5,0,0,5" ItemsSourc...

Show Detail