开发者

WPF CLR Type to UIElement

I am trying to sol开发者_开发百科ve a complicated problem. I am building a dynamic interface and want to convert any existence of an ArrayList into a TreeView. I tried value converter but it did not work.

Here is my code:

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

The ArrayList has items that are Dictionary<String,Object> and then the dictionary has items.


Is there a reason not to do this declaratively? You can create a template for your items pretty easily:

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

I'll confess that I don't know for certain that the bindings defined above will work. But assuming they do, the only other problem you have is applying this template only to items whose Value is an ArrayList. You should be able to write a template selector that does that and assign it to GridViewColumn.CellTemplateSelector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜