如何设置一个WPF DataTemplate中的$ C $下一个TreeView?如何设置、WPF、TreeView、DataTemplate

由网友(独撑全场)分享简介:struct Drink{public string Name { get; private set; }public int Popularity { get; private set; }public Drink ( string name, int popularity ): this ( ){this.Nam...
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...!)

阅读全文

相关推荐

最新文章