简体   繁体   中英

Binding a WPF TreeView control to a KeyValuePair<string,object>

I would like to have my treeview built up of KeyValuePair and only display the key as the header. I have googled this and cant find any examples.

So far I have:

KeyValuePair<string, object> str = new KeyValuePair<string,object> (cores.Keys[i], cores.Values[i]);
TreeViewItem tvi = new TreeViewItem();
tvi.Header = str;

then in the xaml: <TreeView Name="tvCores" Grid.Column="0" PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown" DisplayMemberPath="Key"/ >

Please let me know if you need anymore information

In your code behind, you just need to do this :

KeyValuePair<string, object> str = new KeyValuePair<string, object>(cores.Keys[i], cores.Values[i]);
List<KeyValuePair<string, object>> list = new List<KeyValuePair<string, object>>();
list.Add(str);
tvCores.ItemsSource = list;

Now, your ItemsSource is a list of KeyValuePair so the path work, before the ItemsSource was a TreeViewItem so the path couldn't work.

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