【问题标题】:WPF CLR Type to UIElementWPF CLR 类型到 UIElement
【发布时间】:2011-01-23 23:29:48
【问题描述】:

我正在尝试解决一个复杂的问题。我正在构建一个动态接口,并希望将任何存在的ArrayList 转换为TreeView。我尝试了值转换器,但它不起作用。

这是我的代码:

 if(current.Value is ArrayList)
 {
     var factory = new FrameworkElementFactory(typeof (TreeView));

     factory.SetBinding(TreeView.ItemsSourceProperty, new Binding("[" + current.Key + "]"));
     factory.SetBinding(TreeView.DisplayMemberPathProperty, new Binding("[" + current.Key + "][0].Text")); 

     template = new DataTemplate() {VisualTree = factory}; 
 }

 var column = new GridViewColumn
                  {
                      Header = current.Key,
                      CellTemplate = template
                  };

 gridView.Columns.Add(column);

ArrayListDictionary<String,Object> 的项目,然后字典有项目。

【问题讨论】:

  • 您尝试过 DataTemplate 的 DataType 为 ArrayList 吗?

标签: wpf uielement


【解决方案1】:

是否有理由不以声明方式执行此操作?您可以很容易地为您的项目创建模板:

<DataTemplate x:Key="ArrayListTemplate">
   <TreeView>
      <TreeView.ItemsSource>
         <Binding Source="[{Binding Key}]"/>
      </TreeView.ItemsSource>
      <TreeView.DisplayMemberPath>
         <Binding Source="[{Binding Key}][0].Text"/>
      </TreeView.DisplayMemberPath>
   </TreeView>
</DataTemplate>

我承认我不确定上面定义的绑定是否会起作用。但是假设他们这样做了,您遇到的唯一其他问题就是将此模板仅应用于ValueArrayList 的项目。您应该能够编写一个模板选择器来执行此操作并将其分配给GridViewColumn.CellTemplateSelector

【讨论】:

  • 我使用了上述技术,但绑定不起作用。我会在几个小时后发布一些代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 2012-06-14
  • 2020-06-16
  • 1970-01-01
  • 2023-04-03
  • 2014-07-31
相关资源
最近更新 更多