Question

Having an issue with selection of my treeview. The idea is that the user clicks the item and data is added under the node that they selected

    public MainWindow()
    {
        InitializeComponent();
        this.UserControl1.TreeView1.SelectedItemChanged += new RoutedPropertyChangedEventHandler<Object>(InterfaceTreeViewComputers_SelectionChange);
    }

    void InterfaceTreeViewComputers_SelectionChange(Object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        var MyTreeView = MainWindow.UserControl1.Treeview1.Items.SourceCollection;
        var TheSource = sender as TreeView;
        var TheProperty = e.Source;
        var ThePropertyAsTreeView = TheProperty as TreeView

        TreeViewItem item = e.OriginalSource as TreeViewItem; //Equals Null
        var Attempt2 = ThePropertyAsTreeView.SelectedItem //Equals Null
        var Attempt3 = TheSource.SelectedItem as TreeViewItem //Equals Null
        var Attempt4 = TheSource.SelectedItem //Equals onbject(String)
    }

It seems that the selected item is a textblock and i cant seem to find a way to get it as a treeview item to add nodes under it.

Yes i am pretty new to this type of programming.

Thank you for any help you may provide.

Was it helpful?

Solution

Try this to get selected item:

TreeViewItem item = e.NewValue as TreeViewItem;

Or this to get previously selected item:

TreeViewItem item = e.OldValue as TreeViewItem;

e.Source and e.OriginalSource refer to the TreeView not the TreeViewItem selected. You can use breakpoint then see those properties value and type in Visual Studio's watch. That what I was doing before posting this.

OTHER TIPS

I believe you are approaching this the wrong way. Most of your problems will disappear if you use DataBinding to populate your TreeView. Take into account the following class

namespace StackOverflow._20716616
{
    public class Model
    {
        public Model() 
        { Items = new ObservableCollection<Model>(); }
        public Model(string text) 
            : this() 
        { Text = text; }
        public string Text { get; set; }
        public ObservableCollection<Model> Items { get; set; }
    }
}

Using this class as the model of an MVVM pattern, I can create a hierarchical object graph which I can bind to the TreeView using ItemsSource and a HierarchicalDataTemplate like

<TreeView ItemsSource="{Binding Path=Items}" SelectedItemChanged="TreeView_OnSelectedItemChanged">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate DataType="{x:Type this:Model}" ItemsSource="{Binding Path=Items}">
            <TextBlock Text="{Binding Path=Text}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

The DataContext of the window was set up on the root node of the xaml

DataContext="{Binding RelativeSource={RelativeSource Self}}"

In the code behind, I populate the root items of the TreeView

public MainWindow()
{
    Items = new ObservableCollection<Model>() { new Model("one"), new Model("two"), new Model("three") };
    InitializeComponent();
}

public ObservableCollection<Model> Items { get; set; }

Then, in the SelectedItemChanged, I can just add the children to the object graph

private void TreeView_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    var model = e.NewValue as Model;

    if (!model.Items.Any())
    {
        new List<Model>() { new Model("one"), new Model("two"), new Model("three") }
            .ForEach(model.Items.Add);
    }

    // expand the selected item.
    var item = ((TreeView)sender).ItemContainerGenerator.ContainerFromItem(model) as TreeViewItem;
    item.IsExpanded = true;
}

Note, this is poor man's MVVM where I have used the Windows code behind as the ViewModel. I have also left out null checks and exception catching for the sake of brevity.

I hope this helps

Your right, i am so sorry. http://www.dotnetperls.com/ have given me heaps of insite on this language.

Im too used to programming in things like Visual Basic and Delphi. ive realize now that my MainWindow needs to perform all the pysical actions to my front end user interface and the classes in the other namespaces do not do this.

I have realize that you call a class to perform a specific operation. the class then either returns the information or stores the information in a public property. in which the MainWindow can utilize this and perform the Action required. This i didnt realizeand was trying to manipulate my User Interface from other classes.

I have since restructed my project and the information that was provided here has helped me with my events.

now i have UI coded in XAML Main Window (Performin events and updating the UI) Other Classes (Getting data, or designing controls) so the Main Window can add the control or the data to the UI.

I reel like a nub now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top