如何设置一个WPF DataTemplate中的$ C $下一个TreeView? [英] How to setup a WPF datatemplate in code for a treeview?

查看:138
本文介绍了如何设置一个WPF DataTemplate中的$ C $下一个TreeView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct Drink
{
    public string Name { get; private set; }
    public int Popularity { get; private set; }

    public Drink ( string name, int popularity )
        : this ( )
    {
        this.Name = name;
        this.Popularity = popularity;
    }
}

List<Drink> coldDrinks = new List<Drink> ( ){
    new Drink ( "Water", 1 ),
    new Drink ( "Fanta", 2 ),
    new Drink ( "Sprite", 3 ),
    new Drink ( "Coke", 4 ),
    new Drink ( "Milk", 5 ) };
        }
    }

所以,我可以看到的TreeView项名称的名称属性。

So that I can see the Name property for treeview item names.

推荐答案

里德已经涵盖了构建自己的XAML的做法,而只是提供的FrameworkElementFactory方法的一个例子,它会是这个样子。

Reed has already covered the "build your own XAML" approach, but just to provide an illustration of the FrameworkElementFactory approach, it would look something like this.

首先,创建FEF:

var fef = new FrameworkElementFactory(typeof(TextBlock));
fef.SetBinding(TextBlock.TextProperty, new Binding("Name"));

然后创建一个DataTemplate,其VisualTree设置为工厂:

Then create a DataTemplate with its VisualTree set to that factory:

DataTemplate dt = new DataTemplate { VisualTree = fef };

尽管里德注意到FrameworkElementFactory方法是pcated正式去$ P $,它仍然是相当广泛的应用,我想是因为建设XAML字符串感觉很缺憾。 (虽然FEF方法,如果你有一个不平凡的模板,迅速成为疯狂的复杂...!)

Although as Reed notes the FrameworkElementFactory approach is officially deprecated, it is still reasonably widely used, I guess because building XAML strings feels so kludgy. (Though the FEF approach rapidly becomes insanely complicated if you have a non-trivial template...!)

这篇关于如何设置一个WPF DataTemplate中的$ C $下一个TreeView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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