如何将SortedDictionary绑定到WPF中的TreeView [英] How do I bind a SortedDictionary to TreeView in WPF

查看:72
本文介绍了如何将SortedDictionary绑定到WPF中的TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个排序字典属性:

Hello,
I have a sorted dictionary property:

public SortedDictionary<DateTime, bool> Days { get; set; }



如何将此词典的键名绑定到TreeView子项,最好没有MVVM的复杂性?

根项应该是来自另一个对象的字符串属性。


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.

推荐答案

您好,



您可以使用Itemtemplate属性执行此操作。



Hi,

you can use Itemtemplate property to do this.

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



如果你在treeviewItems中有不同类型的对象,你可以使用树视图项模板的模板选择器(如果你必须绑定除排序字典之外的其他类型)。



Vineeth


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


使用以下代码为xaml,

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



在代码后面使用以下代码将已排序的字典分配给treeview


Use below code in code behind to assign sorted dictionary to treeview

SortedDictionary<DateTime, bool> TestDictionary = new SortedDictionary<DateTime, bool>();
                TestDictionary.Add(DateTime.Today, true);
                TestDictionary.Add(DateTime.Now, false);
                TestTreeView.ItemsSource = TestDictionary;


这篇关于如何将SortedDictionary绑定到WPF中的TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆