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


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

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

  

我没有在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中为treeview动态创建datatemplate。

上述就是C#学习教程:如何在C#后面的代码中动态创建数据模板并绑定树视图层次结构数据分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 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; } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1006062.html

(0)
上一篇 2021年12月28日
下一篇 2021年12月28日

精彩推荐