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



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

<TreeView x:Name="BimTreeView">
</TreeView>

我没有在 XAML 中定义数据模板和绑定定义。因为数据模板和绑定定义必须根据用户的首选项进行更改。

我尝试了在此处找到的以下 C# 代码来动态创建数据模板定义。但是,查看以下代码,我无法弄清楚如何通过 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# 后面的代码中绑定树视图分层数据,我将不胜感激。

谢谢!!

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

最新更新