Quantcast
Viewing all articles
Browse latest Browse all 5

Problem with TreeView

Please show us how you build your collection and what the class looks like.

PartsList seems an odd name for a class but whatever contains your VendorPartNoLink etc.

One thing I'm particularly interested to see is whether it has a collection within it or you're just kind of expecting it to work out what the children are.
Because it won't.

You need something like a converter on your binding.

Like here:

http://stackoverflow.com/questions/14161963/how-to-bind-self-referencing-table-to-wpf-treeview

<TreeView Name="treeview1" ItemsSource="{Binding Converter={StaticResource HierarchyConverter}}" ItemTemplate="{StaticResource ItemTemplate}" ><TreeView.Resources><local:HierarchyConverter x:Key="HierarchyConverter" /><HierarchicalDataTemplate x:Key="ItemTemplate" ItemsSource="{Binding Converter={StaticResource HierarchyConverter}}"><TextBlock Text="{Binding element_name}" /></HierarchicalDataTemplate></TreeView.Resources></TreeView>

And that converter gets the children:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var item = value as MyTable;
        return  item.MyTable1.Where(i => i.parent_id== item.id); //return children
    }

Personally, I prefer to translate into an object containing a child collection.

One of these presents units. A unit can have subunits each subunit contains a unit... and so on.

My itemtemplate presents a unit and the hierarchicaldatatemplate looks:

<HierarchicalDataTemplate
                       ItemsSource="{Binding Path=Unit.SubUnits}"
                       DataType="{x:Type model:SubUnit}"

In Unit:

        private ObservableCollection<SubUnit> _SubUnits;
        public ObservableCollection<SubUnit> SubUnits
        {
            get
            {
                if (_SubUnits == null)
                    _SubUnits = new ObservableCollection<SubUnit>();
                return _SubUnits;
            }
            set { _SubUnits = value; }
        }

SubUnit has a few properties like a GUID and

        public Unit Unit
        {
            get
            {
                return _Unit;
            }
            set
            {
                _Unit = value;
                OnPropertyChanged("Unit");
            }
        }



Hope that helps.

Technet articles: WPF: MVVM Step 1; All my Technet Articles

Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>