【问题标题】:How to dynamically create data template and bind treeview hierarchical data in code behind C#如何在 C# 后面的代码中动态创建数据模板并绑定树视图分层数据
【发布时间】:2018-05-30 16:51:02
【问题描述】:

我有一个场景,其中树视图会动态更改其数据模板和数据绑定定义。 我在 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# 后面的代码中更改数据模板和绑定树视图分层数据,我将不胜感激。

谢谢!!

【问题讨论】:

  • 你期待什么?这个函数应该返回一个可以在你的TreeViewItem 上使用的数据模板实例。您可以只修改该实例。
  • 我需要数据模板,以及更改数据绑定定义的能力。例如,在当前函数中,图像源属性与“ImageURL”绑定。但我无法弄清楚“ImageURL”值的来源。
  • 我认为你选择了错误的方式。你为什么需要这样的东西?
  • Because the data template and binding definition must have to be changed by user's preference.你能分享更多关于这个的信息吗?
  • 此函数返回数据模板,因此您已经拥有它。在大多数情况下,您使绑定实例动态化,而不是试图“重新绑定”它。您可以通过BindingOperations.ClearBinding 清除您的绑定,并使用FindName 访问您的labelimage。但是,我建议您改为制作 ViewModel。

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


【解决方案1】:

这是对上述代码的补救措施。下面的代码现在可以动态绑定,可以为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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多