locked
Treeview parent node selection issue.... RRS feed

  • Question

  • Hi,

    I'm facing an issue with parent node selection,It just looks like a combobox inside a treeview, but i'm using a normal textbox to display text when clicked on child node and a toggle button to open the treeview and treeview is present inside a popup control.

    I have a popup using treeview and text is displayed in a textbox and toggle button to a side.

    when selected parent node the data present in the textbox is erased, when parent node is selected it should not effect the data present in the textbox. only when child not is selected data should change in the textbox, so please help me out with this treeview parent node selection issue.

    Thanks in advance...

    Monday, September 15, 2014 7:07 AM

Answers

  • Hi hiranmayee,

    >>actually my scenario is it should show only child node

    We can check if this item has child items and loop through all items:

    private void DispalyInfo(TreeViewItem newitem)
    {
                if (newitem.Items.Count != 0)
                {
                    string info = "";
                    foreach (TreeViewItem tvi in newitem.Items)
                    {
                        info += tvi.Header + "||";
                    }
    
                    tb1.Text = info;
                }
                else
                    tb1.Text = newitem.Header.ToString();
    }

    >>when parent node is selected it should not erase the childnode details and should not show the parent node in the textbox

    We can create a variable to record the older TreeViewItem by handling TreeView.PreviewMouseLeftButtonDown event:

    <TreeView x:Name="Tree" MouseUp="Tree_MouseUp" Background="LightGray" PreviewMouseLeftButtonDown="Tree_PreviewMouseLeftButtonDown" >
    TreeViewItem priorTVItem;
    private void Tree_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
                if (Tree.SelectedItem != null)
                {
                    priorTVItem = Tree.SelectedItem as TreeViewItem;
                    //Console.WriteLine(priorTVItem.Header);
                }
    }


    Then, in the TreeView.MouseUp event, check if the current item is the parent of older item:

    private void Tree_MouseUp(object sender, MouseButtonEventArgs e)
    {
                TreeViewItem newitem = Tree.SelectedItem as TreeViewItem;
    
                if (newitem != null & priorTVItem == null)
                {
                    DispalyInfo(newitem);
                }
    
                if (newitem != null & priorTVItem!=null)
                {
                    ItemsControl parent = ItemsControl.ItemsControlFromItemContainer(priorTVItem);
                    //Check if this new item is the parent of older item
                    if (parent != null & parent != newitem)
                    {
                        DispalyInfo(newitem);
                    }
                }
    
    }

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 17, 2014 7:55 AM

All replies

  • Hi hiranmayee,

    >>when selected parent node the data present in the textbox is erased, when parent node is selected it should not effect the data present in the textbox

    I think it is related to the binding of your TextBox, could you please provide some code snippets for troubleshooting?

    If possible, a reproducible sample will be better, you could upload it to OneDrive and share with us.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 16, 2014 6:07 AM
  • Hi Franklin,

    When  i to recreate the sample i'm facing a new issue i.e, when i select parent node also it is showing the node details in Textbox, actually my scenario is it should show only child node and when parent node is selected it should not erase the childnode details and should not show the parent node in the textbox.

    So, here is the sample that i created:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace sample_for_treeview
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                
                MouseDown += (sender, args) =>
                {
                    if (args.LeftButton == MouseButtonState.Pressed)
                        DragMove();
                };

                Topmost = true;
            }

           
            private void Tree_MouseUp(object sender, MouseButtonEventArgs e)
            {
               
                   tb1.Text= Tree.SelectedItem.ToString();
              
            }

            private void tb1_SelectionChanged(object sender, RoutedEventArgs e)
            {
     
                string selIx = Tree.SelectedItem.ToString();
                if (selIx != null)
                    popup1.IsOpen = true;
                else
                    popup1.IsOpen = false;
           
            }

            private void btn1_MouseEnter(object sender, MouseEventArgs e)
            {
                popup1.StaysOpen = true;
            }

            private void btn1_MouseLeave(object sender, MouseEventArgs e)
            {
                popup1.StaysOpen = false;
            }

        }
    }
     i created this sample based on my project only rather using a treeview inside a combobox i'm using treeview inside a popup.

    Thanks...

    Hiranmayee

    Tuesday, September 16, 2014 10:13 AM
  • Design:

    <Window x:Class="sample_for_treeview.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="90" Width="520" WindowStyle="None" WindowStartupLocation="CenterScreen"  ShowInTaskbar="False" BorderThickness="0.5" Opacity="0.7" Background="LightGray">
        <Grid>
           
            <TextBox Name="tb1" Margin="0,50,115,4" Background="White" SelectionChanged="tb1_SelectionChanged"></TextBox>
            <ToggleButton Background="Chocolate" Name="btn1" Margin="467,54,22,4" RenderTransformOrigin="-1.083,0.483" Content="^" IsChecked="{Binding IsChecked, ElementName=popup1, Mode=TwoWay}"
                          MouseEnter="btn1_MouseEnter" MouseLeave="btn1_MouseLeave">
                <ToggleButton.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform ScaleX="-1"/>
                        <SkewTransform/>
                        <RotateTransform/>
                        <TranslateTransform/>
                    </TransformGroup>
                </ToggleButton.RenderTransform>
            </ToggleButton>
            <Popup Name="popup1" IsOpen="{Binding IsChecked, ElementName=btn1, Mode=TwoWay}"  StaysOpen="False" Margin="0,50,374,244" Height="auto" Width="auto">
                <Grid Background="Gray">
                    <TreeView x:Name="Tree" MouseUp="Tree_MouseUp" Background="LightGray">

                        <TreeViewItem Header="One">

                            <TreeViewItem Header="One-1">

                                <TreeViewItem Header="One-1-1"/>

                            </TreeViewItem>

                            <TreeViewItem Header="One-2">

                                <TreeViewItem Header="One-2-1"/>

                            </TreeViewItem>

                            <TreeViewItem Header="One-3">

                                <TreeViewItem Header="One-3-1"/>

                            </TreeViewItem>

                            <TreeViewItem Header="One-4">

                                <TreeViewItem Header="One-4-1"/>

                            </TreeViewItem>

                        </TreeViewItem>

                        <TreeViewItem Header="Two">

                            <TreeViewItem Header="Two-1">

                                <TreeViewItem Header="Two-1-1"/>

                            </TreeViewItem>

                            <TreeViewItem Header="Two-2">

                                <TreeViewItem Header="Two-2-1"/>

                            </TreeViewItem>

                        </TreeViewItem>

                    </TreeView>
                </Grid>
            </Popup>
        </Grid>
    </Window>

    Tuesday, September 16, 2014 10:14 AM
  • Hi hiranmayee,

    >>actually my scenario is it should show only child node

    We can check if this item has child items and loop through all items:

    private void DispalyInfo(TreeViewItem newitem)
    {
                if (newitem.Items.Count != 0)
                {
                    string info = "";
                    foreach (TreeViewItem tvi in newitem.Items)
                    {
                        info += tvi.Header + "||";
                    }
    
                    tb1.Text = info;
                }
                else
                    tb1.Text = newitem.Header.ToString();
    }

    >>when parent node is selected it should not erase the childnode details and should not show the parent node in the textbox

    We can create a variable to record the older TreeViewItem by handling TreeView.PreviewMouseLeftButtonDown event:

    <TreeView x:Name="Tree" MouseUp="Tree_MouseUp" Background="LightGray" PreviewMouseLeftButtonDown="Tree_PreviewMouseLeftButtonDown" >
    TreeViewItem priorTVItem;
    private void Tree_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
                if (Tree.SelectedItem != null)
                {
                    priorTVItem = Tree.SelectedItem as TreeViewItem;
                    //Console.WriteLine(priorTVItem.Header);
                }
    }


    Then, in the TreeView.MouseUp event, check if the current item is the parent of older item:

    private void Tree_MouseUp(object sender, MouseButtonEventArgs e)
    {
                TreeViewItem newitem = Tree.SelectedItem as TreeViewItem;
    
                if (newitem != null & priorTVItem == null)
                {
                    DispalyInfo(newitem);
                }
    
                if (newitem != null & priorTVItem!=null)
                {
                    ItemsControl parent = ItemsControl.ItemsControlFromItemContainer(priorTVItem);
                    //Check if this new item is the parent of older item
                    if (parent != null & parent != newitem)
                    {
                        DispalyInfo(newitem);
                    }
                }
    
    }

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 17, 2014 7:55 AM
  • This post is quite helpful.

    Thanks..

    Wednesday, September 17, 2014 9:51 AM