This is a migrated thread and some comments may be shown as answers.

Binding to nested dictionary collection issue

1 Answer 117 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
MattC2007
Top achievements
Rank 1
MattC2007 asked on 07 Oct 2010, 02:16 AM
I'm unable to properly display my collection. I have tryed your HierarchicalDataTemplate example without success. Here are my code snippets.

Collection Class 

public Dictionary<int, CategoryNode> Hierarchy = new Dictionary<int,CategoryNode>();

 public void Add(Asset asset)
        {
            if (!Hierarchy.ContainsKey(asset.AssetCategoryId))
            {
                Hierarchy[asset.AssetCategoryId] = new CategoryNode(asset.AssetCategoryId, asset.Type.Category.AssetCategoryName);

                Add(asset);
            }
            else
            {
                CategoryNode ac = Hierarchy[asset.AssetCategoryId];

                if (!ac.Items.ContainsKey(asset.AssetTypeId))
                {
                    ac.Items[asset.AssetTypeId] = new TypeNode(asset.AssetTypeId, asset.Type.AssetTypeName);

                    Add(asset);
                }
                else
                {
                    TypeNode at = ac.Items[asset.AssetTypeId];

                    if (!at.Items.ContainsKey(asset.AssetId))
                    {
                        at.Items[asset.AssetId] = new AssetNode(asset.AssetId, asset.AssetName); 

                        Add(asset);
                    }
                    else
                    {
                        at.Items[asset.AssetId].Value = asset.AssetName;
                    }

                    //Update Master List
                    List[asset.AssetId] = asset;
                }
            }
        }

        public List<int> GetAssetList()
        {
            List<int> assetList = new List<int>();

            foreach (var item in List.Values)
            {
                assetList.Add(item.AssetId); 
            }

            return assetList;
        }

        public List<int> GetAssetListByAssetCategory(int categoryID)
        {
            List<int> results = new List<int>();

            if (Hierarchy.ContainsKey(categoryID))
            {
                CategoryNode ac = Hierarchy[categoryID];

                foreach (TypeNode at in ac.Items.Values)
                {
                    foreach (AssetNode node in at.Items.Values)
                    {
                        results.Add(node.Key);
                    }
                }
            }

            return results;
        }

        public List<int> GetAssetListByAssetType(int categoryID, int typeID)
        {
            List<int> results = new List<int>();

            if (Hierarchy.ContainsKey(categoryID))
            {
                CategoryNode ac = Hierarchy[categoryID];

                if (ac.Items.ContainsKey(typeID))
                {
                    TypeNode at = ac.Items[typeID];

                    foreach (AssetNode node in at.Items.Values)
                    {
                        results.Add(node.Key);
                    }
                }
            }

            return results;
        }

        public bool Remove(int assetID)
        {
            bool result = false;

            if (List.ContainsKey(assetID))
            {
                Asset asset = List[assetID];

                if (Hierarchy.ContainsKey(asset.AssetCategoryId))
                {
                    CategoryNode ac = Hierarchy[asset.AssetCategoryId];

                    if (ac.Items.ContainsKey(asset.AssetTypeId))
                    {
                        TypeNode at = ac.Items[asset.AssetTypeId];

                        if (at.Items.ContainsKey(asset.AssetId))
                        {
                            //Remove from hierarchy
                            at.Items.Remove(asset.AssetId);

                            //Remove from master list
                            List.Remove(asset.AssetId);

                            //success
                            result = true;
                        }
                        else
                        {
                            return false;
                            //throw new Exception("Asset " + asset.AssetId.ToString() + "exists in main list but the asset is missing in the hierarchy");
                        }
                    }
                    else
                    {
                        return false;
                        //throw new Exception("Asset " + asset.AssetId.ToString() + "exists in main list but the asset type is missing in the hierarchy");
                    }
                }
                else
                {
                    return false;
                    //throw new Exception("Asset " + asset.AssetId.ToString() + "exists in main list but the asset category is missing in the hierarchy");
                }
            }
            else
            {
                //Asset doesn't exist in master list
                return false;
            }

            return result;
        }
    }

    public class CategoryNode
    {
        public int Key { get; set; }
        public string Value { get; set; }
        public Dictionary<int, TypeNode> Items { get; set; }

        public CategoryNode(int key, string value)
        {
            Key = key;
            Value = value;
            Items = new Dictionary<int, TypeNode>();
        }
    }

    public class TypeNode
    {
        public int Key { get; set; }
        public string Value { get; set; }
        public Dictionary<int, AssetNode> Items { get; set; }

        public TypeNode(int key, string value)
        {
            Key = key;
            Value = value;
            Items = new Dictionary<int, AssetNode>();
        }
    }

    public class AssetNode
    {
        public int Key { get; set; }
        public string Value { get; set; }

        public AssetNode(int key, string value)
        {
            Key = key;
            Value = value;
        }
    }



XAML Page

usercontrol resources..

<DataTemplate x:Key="NodeAssets">
                <TextBlock Text="{Binding Path=Value.Value}" />
        </DataTemplate>

            <telerik:HierarchicalDataTemplate x:Key="NodeAssetTypes" ItemTemplate="{StaticResource NodeAssets}" ItemsSource="{Binding AssetNode, Path=Value}">
                <TextBlock Text="{Binding Path=Value.Value}" />
            </telerik:HierarchicalDataTemplate>

            <telerik:HierarchicalDataTemplate x:Key="NodeAssetCategories" ItemTemplate="{StaticResource NodeAssetTypes}" ItemsSource="{Binding TypeNode, Path=Value}">
                <TextBlock Text="{Binding Path=Value.Value}" />
            </telerik:HierarchicalDataTemplate>


TreeView..

<telerikNavigation:RadTreeView x:Name="tvAssets" SelectionMode="Single" IsExpandOnSingleClickEnabled="True" IsDragDropEnabled="False" IsRootLinesEnabled="True" IsLineEnabled="True" Foreground="White" 
                                                               Background="#646464" Selected="tvAssets_Selected" ItemTemplate="{StaticResource NodeAssetCategories}">
</telerikNavigation:RadTreeView>

Code Behind

tvAssets.ItemsSource = MapAssets.Hierarchy;


1 Answer, 1 is accepted

Sort by
0
MattC2007
Top achievements
Rank 1
answered on 08 Oct 2010, 04:58 PM
Nevermind I fixed the issue. I ended up giving my classes more meaningfull names for their properties. I was using key/value properties for my class members. Instead I used ID, Name, CollectionType. I also implemented Inotification interface and everything is working perfecty!

Here are my datatemplates (alittle different then most examples, because I use nested dictionaries..)

<DataTemplate x:Key="NodeAssets">
                <TextBlock Text="{Binding Path=Name}" />
            </DataTemplate>
            <telerik:HierarchicalDataTemplate x:Key="NodeAssetTypes" ItemTemplate="{StaticResource NodeAssets}" ItemsSource="{Binding Path=Assets.Values}" ItemContainerStyle="{StaticResource RadTreeViewItemStyle1}" >
                <TextBlock Text="{Binding Path=Name}" />
            </telerik:HierarchicalDataTemplate>
            <telerik:HierarchicalDataTemplate x:Key="NodeAssetCategories" ItemTemplate="{StaticResource NodeAssetTypes}" ItemsSource="{Binding Path=Types.Values}" ItemContainerStyle="{StaticResource RadTreeViewItemStyle1}">
                <TextBlock Text="{Binding Path=Name}" />
            </telerik:HierarchicalDataTemplate>


Tags
TreeView
Asked by
MattC2007
Top achievements
Rank 1
Answers by
MattC2007
Top achievements
Rank 1
Share this question
or