1
votes

I am trying to solve 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.

1
Did you try a DataTemplate with DataType as ArrayList ?Gishu

1 Answers

1
votes

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.