0

I am trying to represent information from related datatables customers<-products into a treeview.

Due to performance issues I wanted to use Backgroundworker, but there is a gotcha - no GUI units (also TreeViewItem in other thread than STA possible).

So I defined my own "STA-thread-safe" class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace XXX.YYY
{
public class TreeViewNode: INotifyPropertyChanged
{
    #region [ Header ]
    private string _header;
    public string Header
    {
        get
        {
            return _header;
        }

        set
        {
            _header = value;
            OnNotifyPropertyChanged("Header");
        }
    } 
    #endregion

    #region [ Tag ]
    private string _tag;
    public string Tag
    {
        get
        {
            return _tag;
        }

        set
        {
            _tag = value;
            OnNotifyPropertyChanged("Tag");
        }
    } 
    #endregion

    #region [ Items ]
    private ObservableCollection<TreeViewNode> _items;

    public ObservableCollection<TreeViewNode> Items
    {
        get
        {
            if (_items == null)
            {
                _items = new ObservableCollection<TreeViewNode>();
            }

            return _items;
        }

        set
        {
            _items = value;
            OnNotifyPropertyChanged("Items");
        }
    } 
    #endregion

    #region [ Property_Changed_Utilities ]
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnNotifyPropertyChanged(String info)
    {
        // Note: Do not forget to add interface 
        // "INotifyPropertyChanged" to your class.
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    #endregion

    public TreeViewNode()
    {         
    }

}
}

And an Template in XAML for representing:

<TreeView Name="treeview"
      ItemsSource="{Binding RelativeSource=
          {RelativeSource Mode=FindAncestor,AncestorType=Window},
      Path=TreeViewData}"
      SelectedItemChanged="treeview_SelectedItemChanged">

    <TreeView.Resources>
        <Style TargetType="TreeViewItem">
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TreeViewItem Header="{Binding Header}" 
                                      ItemsSource="{Binding Items}" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TreeView.Resources>
    <TreeView.ItemTemplate>
        <DataTemplate>
            <TreeViewItem Header="{Binding Header}" 
                          ItemsSource="{Binding Items}" />
        </DataTemplate>
    </TreeView.ItemTemplate>

It is working so far - showing populated tree with subitems as wished. Also expanding/collapsing works...

BUT - no event is risen - there is no reaction on SelectedItemChanged.

Any help appreciated! Thank you very much in advance!

Natxo
  • 2,917
  • 1
  • 24
  • 36
user1608721
  • 136
  • 1
  • 9

2 Answers2

2

Consider adding a Trigger to your treeView (.xaml);

<i:Interaction.Triggers>
   <i:EventTrigger EventName="Selected">
       <cmdex:EventToCommand Command="{Binding ExecutionCommand}"
        PassEventArgsToCommand="True" />
     </i:EventTrigger>
</i:Interaction.Triggers>

Where

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

and

xmlns:cmdex="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"

Then you can code your command easily. The only difference is that I'm using a Telerik treeview but I suppose the same thing can be done to a .NET treeview.

Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
  • Sorry to say but I was not able to find the needed instances ... Where "i:" and "cmdex:" are defined... I have also with tried EventSetter but to no avail... – user1608721 Sep 24 '13 at 07:46
  • Thank you very much for your time, i solved it myself, but your hint gave me the right direction where to look for... – user1608721 Sep 24 '13 at 08:21
  • You're the most welcome ;) My answer was meant to give hints, as we weren't using the same control. – Nadeem_MK Sep 24 '13 at 08:35
0

I solved it myself, thanks to the hint of Nadeem_MK...

First I used event setter to add simple click event poping up message box with exclamation mark. This worked showing strange effect - I stil had parent element, that can be expanded, but after click the whole group (parent with expanded children was highlighted)...

Also it is an issue with the template representation - not an hierarchical view, but just list of colapsing elements...

So here is the solution - pure XAML - based on http://blogs.msdn.com/b/mikehillberg/archive/2009/10/30/treeview-and-hierarchicaldatatemplate-step-by-step.aspx

            <TreeView Name="treeview"
                      ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                                                           AncestorType=Window},
                                            Path=TreeViewData}"
                      SelectedItemChanged="treeview_SelectedItemChanged">

                <!--  Supplier teamplate  -->
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Items}">
                        <TextBlock Text="{Binding Header}" />

                        <!--  Products template  -->
                        <HierarchicalDataTemplate.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Header}" />
                            </DataTemplate>
                        </HierarchicalDataTemplate.ItemTemplate>

                    </HierarchicalDataTemplate>

                </TreeView.ItemTemplate>

            </TreeView>
user1608721
  • 136
  • 1
  • 9