views:

239

answers:

4

Could you tell me why I can't see subItems?

I've got winforms apps and I added my wpfusercontrol:ObjectsAndZonesTree

ServiceProvider is my webservice. Adn method to get listofcountires with subitems works properly (i get countires, regions from this countires, provinces etc...)

 ElementHost elementHost = new ElementHost
                                          {
                                              Width = 150,
                                              Height = 50,
                                              Dock = DockStyle.Fill,
                                              Child = new ObjectsAndZonesTree()
                                          };


            this.splitContainer3.Panel1.Controls.Add(elementHost);

XAML:

<TreeView Name="GroupView" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type ServiceProvider:Country
}" ItemsSource="{Binding Items}">
                <TextBlock Text="{Binding Path=Name}" />
                </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type ServiceProvider:Region}" >
                <TextBlock Text="{Binding Path=Name}" />
                </DataTemplate>
            <DataTemplate DataType="{x:Type ServiceProvider:Province}" >
                <TextBlock Text="{Binding Path=Name}" />
                </DataTemplate>
        </TreeView.Resources>
    </TreeView>

XAML.CS

 public ObjectsAndZonesTree()
        {
            InitializeComponent();

            LoadView();
        }

        private void LoadView()
        {
            GroupView.ItemsSource = new ServiceProvider().GetListOfObjectsAndZones();

        }

class Country:

public class Country
    {
        string _name;

        [XmlAttribute]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        string _code;

        [XmlAttribute]
        public string Code
        {
            get { return _code; }
            set { _code = value; }
        }

        string _continentCode;

        [XmlAttribute]
        public string ContinentCode
        {
            get { return _continentCode; }
            set { _continentCode = value; }
        }

        public Region[] ListOfRegions
        {
            get { return _listOfRegions; }
            set { _listOfRegions = value; }
        }

        private Region[] _listOfRegions;

        public IList<object> Items
        {
            get
            {
                IList<object> childNodes = new List<object>();
                foreach (var group in this.ListOfRegions)
                    childNodes.Add(group);

                return childNodes;
            }
        }



    }

Class Region:

public class Region {

private Province[] _listOfProvinces;
        private string _name;
        private string _code;

        public Province[] ListOfProvinces
        {
            get { return _listOfProvinces; }
            set { _listOfProvinces = value; }
        }

        public string Name
        {
            get {
                return _name;
            }
            set {
                _name = value;
            }
        }

        public string Code
        {
            get {
                return _code;
            }
            set {
                _code = value;
            }
        }

        public string CountryCode
        {
            get { return _countryCode; }
            set { _countryCode = value; }
        }

        private string _countryCode;

        public IList<object> Items
        {
            get
            {
                IList<object> childNodes = new List<object>();
                foreach (var group in this.ListOfProvinces)
                    childNodes.Add(group);

                return childNodes;
            }
        }
}

It displays me only list of countires.

A: 

Have you implemented INotifyPropertyChanged on Binding source class??

Plus you can check for binding exceptions in output window of visual studio. It will help you understanding invalid bindings.

Rakesh Gunijan
+2  A: 

Your Region DataTemplate needs to be a HierarchicalDataTemplate to support nested items (SubItems). You also need to specify it's ItemsSource.

<TreeView Name="GroupView" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type ServiceProvider:Country}"
                                  ItemsSource="{Binding Items}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type ServiceProvider:Region}" 
                                  ItemsSource="{Binding Items}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type ServiceProvider:Province}">
            <TextBlock Text="{Binding Path=Name}" />
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

So for example if you add Cities to your Provinces the changes in your XAML might look something like this.

        <HierarchicalDataTemplate DataType="{x:Type ServiceProvider:Province}" 
                                  ItemsSource="{Binding Cities}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>

        <DataTemplate DataType="{x:Type ServiceProvider:City}">
            <TextBlock Text="{Binding Path=Name}" />
        </DataTemplate>
VoidDweller
+1  A: 

Not sure where your problem is, but I thought I would share you the best resource I've found when dealing with Treeview. Thoses extension methods saved me a lot of hassle :

http://www.scip.be/index.php?Page=ArticlesNET23

They transform any flat list into a Ienumerable of HierarchyNode using some nice lambda syntax. It is implemented with IQueryable, which means efficient even against a linq datacontext.

Stephane