简体   繁体   中英

Not able to make bindings in treeview

Im trying to make a custom treeview with an itemtemplate, so I can show the headertext + a type of the item in the treeview.

My inspiration comes from this answer; https://stackoverflow.com/a/33119107/9156219

So, my problem is that I cant make the Itembindings work. Here's my code;

XAML:

<TreeView x:Name="treeView" ItemsSource="{Binding treeList}" Grid.Column="0" IsVisibleChanged="treeView_IsVisibleChanged" SelectedItemChanged="treeView_SelectedItemChanged">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Type}" Margin="2,2,2,2" Background="LightBlue" FontSize='8'/>
                <TextBlock Text="{Binding SystemName}" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

C#

    public class CustomTreeViewItem : TreeViewItem
    {
        public String SystemName { get; set; }
        public String Type { get; set; }
        public String ParentItem { get; set; }
        public String Path { get; set; }
    }

    public List<CustomTreeViewItem> treeList = new List<CustomTreeViewItem>();

    public void SetRootNode()
    {
        int itmNumber = datSet.Rows.Count;

        for (int i = 0; i < itmNumber; i++)
        {
            treeList.Add(new CustomTreeViewItem
            {
                SystemName = (string)datSet.Rows[i].ItemArray[1].ToString(),
                Type = (string)datSet.Rows[i].ItemArray[2].ToString(),
                ParentItem = (string)datSet.Rows[i].ItemArray[3].ToString(),
                Path = (string)datSet.Rows[i].ItemArray[4].ToString(),
            });
            treeList[i].Header = treeList[i].SystemName;
        }


        foreach (CustomTreeViewItem item in treeList.Where(treeList => treeList.ParentItem == ""))
        {
            treeView.Items.Add(item);
        }

        foreach (CustomTreeViewItem item in treeList.Where(treeList => treeList.ParentItem != "").Where(treeList => treeList.Type != "Signal"))
        {
            var test = treeList.Find(treeList => treeList.SystemName == item.ParentItem);
            test.Items.Add(item);
        }

    }

SetRootNode() is being called in the beginning of the program. datSet is being filled with a OleDBDataAdapter.

In the treeview, only the SystemName is being showed and not the type. What am I doing wrong?

Thank you in advance!

You are trying to create a TreeView whose TreeViewItems contain yet again TreeViewItems, if you remove the inheritance for your model, it works:

do this:

public class CustomTreeViewItem

instead of

public class CustomTreeViewItem : TreeViewItem

is this good enough for your needs?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM