简体   繁体   English

如何在树视图的代码中设置WPF datatemplate?

[英]How to setup a WPF datatemplate in code for a 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 ) };
        }
    }

So that I can see the Name property for treeview item names. 这样我就可以看到树视图项名称的Name属性。

There are two approaches. 有两种方法。 The easiest is to just generate the xaml, and parse it at runtime: 最简单的方法是生成xaml,并在运行时解析它:

string xaml = "<DataTemplate><TextBlock Text=\"{Binding Name}\"/></DataTemplate>";
MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
ParserContext pc = new ParserContext();
pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
DataTemplate datatemplate = (DataTemplate)XamlReader.Load(sr, pc);
treeView1.Resources.Add("dt", datatemplate);

The second option is to use the FrameworkElementFactory class . 第二个选项是使用FrameworkElementFactory类 However, this is quite involved, and difficult to get "right". 然而,这是非常复杂的,并且难以“正确”。 As MSDN now refers to this as deprecated, I won't include code to demonstrate... 由于MSDN现在将其称为已弃用,因此我不会包含用于演示的代码...

Instead of creating your own XAML like Reed said you can get a control XAML by using 而不是像Reed那样创建自己的XAML,你可以通过使用获得控件XAML

String myXAML = System.Windows.Markup.XamlWriter.Save(yourControl.Template)

You can then edit the XAML and create back your controltemplate/datatemplate 然后,您可以编辑XAML并创建controltemplate / datatemplate

var xamlStream = new MemoryStream(System.Text.Encoding.Default.GetBytes(myXAML));
_buttonControlTemplate = (ControlTemplate)System.Windows.Markup.XamlReader.Load(xamlStream);

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. Reed已经介绍了“构建自己的XAML”方法,但只是为了提供FrameworkElementFactory方法的说明,它看起来像这样。

First, create the FEF: 首先,创建FEF:

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

Then create a DataTemplate with its VisualTree set to that factory: 然后创建一个DataTmplate,其VisualTree设置为该工厂:

DataTemplate dt = new DataTemplate { VisualTree = 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. 虽然Reed注意到FrameworkElementFactory方法已经被正式弃用,但它仍然被广泛使用,我想因为构建XAML字符串感觉非常糟糕。 (Though the FEF approach rapidly becomes insanely complicated if you have a non-trivial template...!) (尽管如果你有一个非平凡的模板,FEF方法会迅速变得非常复杂......!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM