Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a sorted dictionary property:
C#
public SortedDictionary<DateTime, bool> Days { get; set; }

How do I bind the key names of this dictionary to the TreeView children, preferably without the complications of MVVM?
Root item should be a string property from another object.
Posted

Hi,

you can use Itemtemplate property to do this.

XML
<treeview itemssource="{Binding Days}">            
            <treeview.itemtemplate>
                <datatemplate>
                    <textblock text="{Binding Key}" />
                </datatemplate>
            </treeview.itemtemplate>
        </treeview>

if you have different types of objects in treeviewItems you can use template selector for treeview item template (in case you have to bind other types than sorted dictionary).

Vineeth
 
Share this answer
 
Use the following code for xaml,
XML
<TreeView Name="TestTreeView">
            <TreeView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Key}"/>
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

Use below code in code behind to assign sorted dictionary to treeview
XML
SortedDictionary<DateTime, bool> TestDictionary = new SortedDictionary<DateTime, bool>();
                TestDictionary.Add(DateTime.Today, true);
                TestDictionary.Add(DateTime.Now, false);
                TestTreeView.ItemsSource = TestDictionary;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900