Click here to Skip to main content
12,449,149 members (56,527 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.0 C# WPF Treeview
I have run into a problem. In wpf I have a treeview populated by databinding. The following code is a subset of the complete code.

<Window x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <HierarchicalDataTemplate DataType="{x:Type local:TreeViewItemViewModel}" ItemsSource="{Binding Path=Children}" >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Data}"/>
                <Button Content="Inside" Click="button2_Click" />
        <TreeView Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="treeView1" VerticalAlignment="Top" Width="186" />
        <Button Content="Outside" Height="23" HorizontalAlignment="Left" Margin="204,41,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WpfApplication1
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()
        ObservableCollection<TreeViewItemViewModel> nodes;
        protected override void OnActivated(EventArgs e)
            nodes = new ObservableCollection<TreeViewItemViewModel>();
            nodes.Add(new TreeViewItemViewModel("1"));
            nodes.Add(new TreeViewItemViewModel("2"));
            nodes.Add(new TreeViewItemViewModel("3"));
            TreeViewItemViewModel viewModel = new TreeViewItemViewModel("4");
            viewModel.Parent = nodes[0];
            treeView1.ItemsSource = nodes;
        private void button2_Click(object sender, RoutedEventArgs e)
            TreeViewItemViewModel viewModel = nodes[0].Children[0];
            viewModel.Parent = nodes[1];
            nodes[1].IsExpanded = true;
            viewModel.IsExpanded = true;
            viewModel.IsSelected = true;
    public class TreeViewItemViewModel : INotifyPropertyChanged
        public ObservableCollection<TreeViewItemViewModel> Children { get; private set; }
        private bool _isExpanded;
        public bool IsExpanded
            get { return _isExpanded; }
                if (_isExpanded != value)
                    _isExpanded = value;
                    if (Parent != null)
                        Parent.IsExpanded = true;
        private bool _isSelected;
        public bool IsSelected
            get { return _isSelected; }
                if (_isSelected != value)
                    _isSelected = value;
        public TreeViewItemViewModel Parent { get; set; }
        public TreeViewItemViewModel(string data)
            Data = data;
            Children = new ObservableCollection<TreeViewItemViewModel>();
        public string Data { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

I am trying to move treeviewitem "4" under treeviewitem "2". After the moving I want to select the moved treeviewitem ("4"). If I push the button "outside" or the button "inside" for treeviewitem "3" it works perfect. However, if I push the button "inside" for treeviewitem "4" (the one we are moving), it moves the item, but it does not select it. What am I doing wrong?
Posted 1-Feb-11 5:56am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your button click event has nothing to do with "different" items as it is hard coded to take the first child out of the first node remove it and add it to the second node. So I am thinking it is more order of which you press. (once the effect happend it will happen again on Node 0 first child to Node 1, but it may not be present)

Also I would recomend not using the click event but instead use an ICommand interface [^] and set the button command path to it using binding[^].
The command should exist at the "TreeViewItemViewModel" for the button "inside" as that is the object type for the collection.

As for the "outside" event you have no knowledge of which item is to move so you are forced to do the hard coding that you have. Not sure what your intentions are exactly but you may want to think about if that makes sence or not.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 23 Mar 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100