如何在C#中的代码中动态创建数据模板并绑定树视图层次结构数据 [英] How to dynamically create data template and bind treeview hierarchical data in code behind C#

查看:166
本文介绍了如何在C#中的代码中动态创建数据模板并绑定树视图层次结构数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,其中树视图动态更改其数据模板和数据绑定定义。
我是这样在XAML中创建树形视图的:

I have a scenario, where a treeview changes its data template and data binding definition dynamically. I created a treeview in XAML like this:

<TreeView x:Name="BimTreeView">

</TreeView>

我没有在XAML中定义数据模板和绑定定义。因为必须根据用户的喜好更改数据模板和绑定定义。

I didn't defined the data template and binding definition in XAML. Because the data template and binding definition must have to be changed by user's preference.

我尝试了以下找到的C#代码,此处中进行动态创建数据模板定义。但是,看下面的代码,我不知道如何通过C#代码更改数据绑定定义

I tried the following C# code that I found here to create the data template definition dynamically. However, looking at the following code, I couldn't figure out how to change data binding definition via C# code.

private DataTemplate GetHeaderTemplate()
{
    //create the data template
    DataTemplate dataTemplate = new DataTemplate();

    //create stack pane;
    FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
    stackPanel.Name = "parentStackpanel";
    stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

    // Create check box
    FrameworkElementFactory checkBox = new FrameworkElementFactory(typeof(CheckBox));
    checkBox.Name = "chk";
    checkBox.SetValue(CheckBox.NameProperty, "chk");
    checkBox.SetValue(CheckBox.TagProperty, new Binding());
    checkBox.SetValue(CheckBox.MarginProperty, new Thickness(2));
    checkBox.SetValue(CheckBox.TagProperty, new Binding() { Path = new PropertyPath("Name") });
    stackPanel.AppendChild(checkBox);

    // Create Image 
    FrameworkElementFactory image = new FrameworkElementFactory(typeof(Image));
    image.SetValue(Image.MarginProperty, new Thickness(2));
    image.SetBinding(Image.SourceProperty, new Binding() { Path = new PropertyPath("ImageUrl") });
    stackPanel.AppendChild(image);

    // create text
    FrameworkElementFactory label = new FrameworkElementFactory(typeof(TextBlock));
    label.SetBinding(TextBlock.TextProperty, new Binding() { Path = new PropertyPath("Name") });
    label.SetValue(TextBlock.ToolTipProperty, new Binding());

    stackPanel.AppendChild(label);


    //set the visual tree of the data template
    dataTemplate.VisualTree = stackPanel;

    return dataTemplate;

}

如果有人能解释我如何改变,我将非常感激数据模板,并在C#之后的代码中绑定树视图层次结构数据。

I would really appreciate if someone could explain how can I change the data template and bind treeview hierarchical data in code behind C#.

谢谢!

推荐答案

这是上述解决方法码。以下代码现在可以动态绑定并可以为wpf中的树视图动态创建数据模板。

Here's the remedy to the above code. The following code can now dynamically bind and can dynamically create datatemplate for the treeview in wpf.

public static void FillTree()
{
    BIMExplorerUserControl.Instance.BimTreeView.ItemTemplate = GetTemplate();

    BIMExplorerUserControl.Instance.BimTreeView.ItemsSource = ViewModel.Instance.DefaultExplorerView;
}

public static HierarchicalDataTemplate GetTemplate()
{
    //create the data template
    HierarchicalDataTemplate dataTemplate = new HierarchicalDataTemplate();

    //create stack pane;
    FrameworkElementFactory stackPanel = new FrameworkElementFactory(typeof(StackPanel));
    stackPanel.Name = "parentStackpanel";
    stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

    ////// Create check box
    FrameworkElementFactory checkBox = new FrameworkElementFactory(typeof(CheckBox));
    checkBox.Name = "chk";
    checkBox.SetValue(CheckBox.NameProperty, "chk");
    checkBox.SetValue(CheckBox.TagProperty, new Binding());
    checkBox.SetValue(CheckBox.MarginProperty, new Thickness(2));
    checkBox.SetValue(CheckBox.TagProperty, new Binding() { Path = new PropertyPath("Name") });
    stackPanel.AppendChild(checkBox);


    // create text
    FrameworkElementFactory label = new FrameworkElementFactory(typeof(TextBlock));
    label.SetBinding(TextBlock.TextProperty, new Binding() { Path = new PropertyPath("Name") });
    label.SetValue(TextBlock.MarginProperty, new Thickness(2));
    label.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold);
    label.SetValue(TextBlock.ToolTipProperty, new Binding());

    stackPanel.AppendChild(label);


    dataTemplate.ItemsSource = new Binding("Elements");

    //set the visual tree of the data template
    dataTemplate.VisualTree = stackPanel;

    return dataTemplate;

}

这篇关于如何在C#中的代码中动态创建数据模板并绑定树视图层次结构数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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