如何在C#后面的代码中动态创建数据模板并绑定树视图层次结构数据

时间:2018-05-30 06:02:44

标签: c# .net wpf data-binding treeview

我有一个场景,树视图动态地更改其数据模板和数据绑定定义。 我在XAML中创建了一个树视图,如下所示:

<TreeView x:Name="BimTreeView">

</TreeView>

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

我尝试了以下C#代码,我发现here动态创建数据模板定义。但是,查看以下代码,我无法弄清楚如何通过C#代码更改数据绑定定义

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#后面的代码中绑定树视图分层数据,我将非常感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

以上是对上述代码的补救措施。以下代码现在可以动态绑定,并可以在wpf中为treeview动态创建datatemplate。

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;

}