none
How to add child node to the selected node and refresh after adding in wpf?

    Question

  • HI,

    'm having a treeview which is populated from the database using list collection, it look like this..

    +Drinks

         -Cool

         -Hot

    +Brands

         -Thumsup

         -Pepsi

    And i need to add child nodes to all of these cool, hot, thumsup, pepsi and so on like..

    +Drinks

         +Cool

              -Thumsup

              -Pepsi

         -Hot

    +Brands

         -Thumsup

         -Pepsi

    These are all added based on list collection, if i call treeview binding again after adding it showing new node but all expanded nodes were collapsed. So how to find index for this list collection treeview to refresh treeview with newly added node.

     

    Thanks,

    @nagaraju.


    • Edited by Murali Nagaraju Monday, March 28, 2011 6:22 AM For better understanding
    Monday, March 28, 2011 6:18 AM

Answers

All replies

  • Any one?

     

    Monday, March 28, 2011 9:29 AM
  • Hi,

    you can have a look on following link

    http://www.c-sharpcorner.com/uploadfile/mahesh/wpftreeview08202008231544pm/wpftreeview.aspx

     

    and for your List binding you can hav List<TreeViewItem>


    Thanks & Regards dhampall
    Monday, March 28, 2011 3:08 PM
  • Hi Murali Nagaraju,

    I think dhampall1785 has provide you a solution on how to achieve your goal.

    Additional, I think WPF TreeView is just a Data Display control, so you could separate your TreeView and Data Source, and then you could operate anything you want using the DataSource, becasue of WPF binding, your TreeView will update itself automatically when you operate the data source.

    you could read this article, then you could understand WPF treeview more clearly, the link is:

    http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 28, 2011 3:31 PM
    Moderator
  • Hi Sheldon,

    Thanks for your reply, but the problem is whenever i added new node to the list collection it doesn't display immediately that means doesn't refresh the tree view. I want to know how to find index of tree view, 'm trying like "treeview1.items.indexof(selecteditem)" but it return -1 so tell me how can i get index of tree view.

    Below is the class i created for binding my data with including list collection.

    public class TreeNode
        {
            List<TreeNode> Nodes=new List<TreeNode>();
            public int EntityId { get; set; }
            public string CompanyCode { get; set; }
            public string Category { get; set; }
            public string Name { get; set; }
            public string Value { get; set; }
            public int ParentID { get; set; }
            public List<TreeNode> Items
            {
                get { return Nodes; }
                set { Nodes = value; }
            }
        }

     

    Thanks,

    @nagaraju.

    Tuesday, March 29, 2011 3:48 AM
  • I think the cause of yoru issue:

    -->  the list collection it doesn't display immediately

    is whe your DataSource changed, your UI will not get this Notify(if the binding is two way),

    you could try to change your List to ObservableCollection,  for more details, you could refer to:

    http://msdn.microsoft.com/en-us/library/ms668604.aspx

    http://www.codeproject.com/KB/silverlight/SLListVsOCollections.aspx

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 29, 2011 3:57 AM
    Moderator
  • Hi Sheldon,

    Thanks for your reply, even when 'm taken ObservableCollection i didn't get newly added node immediately please observe my code below.

    This is the class for binding nodes

    public class TreeNode:INotifyPropertyChanged
        {
            ObservableCollection<TreeNode> Nodes = new ObservableCollection<TreeNode>();
            string _Name;
            public int EntityId { get; set; }
            public string CompanyCode { get; set; }
            public string Category { get; set; }
            public string Name {
                get { return _Name; }
                set{_Name=value; onPropertyChanged(this, "Name");}
            }
            public string Value { get; set; }
            public int ParentID { get; set; }
            public ObservableCollection<TreeNode> Items
            {
                get { return Nodes; }
                set { Nodes = value; }
            }
            public event PropertyChangedEventHandler PropertyChanged;
            private void onPropertyChanged(object sender, string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
                }
            }

        }

    This is xaml code..

    <TreeView.ItemTemplate>
                      <HierarchicalDataTemplate ItemsSource="{Binding Path=Items}">
                        <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />                    
                      </HierarchicalDataTemplate>
                    </TreeView.ItemTemplate>
    

    This is code for adding node..

    private void btnTSAdd_Click(object sender, RoutedEventArgs e)
        {  
          TreeNode item = tv.SelectedItem as TreeNode;
          procCal.id = item.EntityId;
          procCal.CompanyCode = txtcCode.Text;
          procCal.Category = "TestSuite";
          procCal.ColName = txtTSName.Text;
          procCal.ColValue = txtTScond.Text;
          int i = procCal.CreateAll();
          //PopulateTreeviewTS();
          brdMainDisp.Margin = new Thickness(241, 4, 5, 4);
          brdTS.Visibility = Visibility.Hidden;
        }
    

    plz check my code and let me know the problem..

     

    Thanks,

    @nagaraju.

     

    Tuesday, March 29, 2011 5:14 AM
  • Thank you for the quick reply.

    It is hard to find the root cause of your issue withi those code snippet, could you upload a zipped file, and then I will help you fix this issue.


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 29, 2011 5:21 AM
    Moderator
  • Hi Sheldon,

    Thanks for your quick reply, could you please tell me how can i send zipped one to you?

     

    Thanks,

    @nagaraju.

    Tuesday, March 29, 2011 5:25 AM
  • 1) type "skydrive.live.com" in the IE address bar.

    2) sign in your account on the page, then click add files to upload your files(remember upload as "public").

    3) give me download link after upload successfully.

    if you could share me a simple app to repro your issue, I think it will be very helpful for me.


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 29, 2011 5:31 AM
    Moderator
  • Hi sheldon,

    i've successfully added my zipped file, plz check below link..

    http://cid-12a251b09b8a1a55.office.live.com/browse.aspx/.Documents?permissionsChanged=1&1

    and for database table and proc's ill put SQL file plz check that one too..

     

    Thanks,

    @nagaraju.

    Tuesday, March 29, 2011 6:12 AM
  • Hi sheldon,

    And also after successfully set the tables and proc's into mysql and run app u'll get tree like..

    +FIX.4.0

        -Equities

        -Options

        -Complx Options

    +FIX.4.1

    +FIX.4.2

    ....

    when you right click on child nodes(equities,options,complxoptions) of FIX.4.0 u'll get context menu create test suite when u click that one 'll popup textboxes with name value after enter name and value, then click add so at that time new node will be added child to the selected one and it'll display immediately.

     

    Thanks,

    @nagaraju.

    Tuesday, March 29, 2011 6:18 AM
  • I could not get your DB successfully, I need hard code data to test your application, so I said I need simple application just now.

    Could you share me a "simple" one?

    On the other hand, based on your description:

    -->  i didn't get newly added node immediately

    If you want to display the node immediately, I think you could try to this method:

    http://social.msdn.microsoft.com/Forums/en-NZ/wpf/thread/d651bd33-53fc-4218-9089-cd77d32f4fd8

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 29, 2011 6:53 AM
    Moderator
  • I think anyone working with wpf  treeview ought to start by reading the link Sheldon posted:

    http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

    When working with this sort of control I also like to use marlon grech's attached command behaviours rather than routed commands.  Because they're attached via style they're easier.

    Tuesday, March 29, 2011 7:38 AM
  • Hi nagaraju,

    How about your issue now?

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 05, 2011 2:08 AM
    Moderator