none
DataTemplate Question

    Question

  • I have a treeview that will eventually drill down to about 30 different datatypes.  What I want to do is have a panel on the right side that shows a representation of the object that was selected in the treeview.

    I would like to do this by creating a datatemplate per object type with one that is the default for object types with no template defined.  Is this possible just by defining the datatemplates and indicating the object type for that datatemplate or do I have to go the TemplateSelector route?

    TIA,

    LS


    Lloyd Sheen

    Saturday, January 05, 2013 10:54 PM

Answers

  • Hi,

    You must define ItemsSource as ObservableCollection of object.

    In XAML resources define DataTemplate for each class.

    For example:

    <DataTemplate DataType="{x:Type my:ClassA}">
       <TextBlock Text="{Binding Path=PropertyA}" />
    </DataTemplate>
    
    <DataTemplate DataType="{x:Type my:ClassB}">
      <TextBlock Text="{Binding Path=PropertyB}" />
    </DataTemplate>

    And in the right side of window define <ContentControl /> element binded to SelectedItem of TreeView:

    <ContentControl Grid.Column="1" 
                    Content="{Binding ElementName=treeView, Path=SelectedValue}" />

    I hope, that I helped you.

    • Marked as answer by sqlguy Sunday, January 06, 2013 6:39 PM
    Sunday, January 06, 2013 4:55 PM

All replies

  • The easiest way to do this is to first set up a class that maps your types like this:

    public class MyTypes{
    
    public string firsttype {get;set;}
    public Object secondtype {get;set;}
    public int thirdtype {get;set;}
    ...
    
    }

    Now create another class like this:

    public class MyTypeCollectionModel {
    
    public ObservableCollection<MyType> OC {get;set;}
    
    }

    Now create a datasource from an object of the class MyTypeCollection...

    Once you have the datasource you can drag it as a grid or detail view onto a form.

    It will map out all the controls necessary to contain a view of the collection.

    Next take the XAML from that view and copy it over to a style within a datatemplate.  Set the datacontext and you're ready to go. 

    Total time = 10 minutes.


    JP Cowboy Coders Unite!

    Sunday, January 06, 2013 12:12 AM
  • Sorry, what I am looking for is a method of displaying DataTemplates within a grid not a datagrid.  The grid would be split in half, the left half having a treeview and the right half having the content that is dependent on which treeview item is selected.

    Each treeview item may be a "label" indicating for instance that under that treeviewitem is a list of tables.  When I select a table the display on the right side will change to show table information.  If the treeview selected is a view then details for the view will be shown on the right side and so on and so on.

    I am looking for the equivalent to having an itemscontrol with multiple object types in its itemssource.  There you can define a datatemplate per object type and when the itemscontrol is rendered it will render each item with its own template.

    LS


    Lloyd Sheen

    Sunday, January 06, 2013 3:13 PM
  • Hi,

    You must define ItemsSource as ObservableCollection of object.

    In XAML resources define DataTemplate for each class.

    For example:

    <DataTemplate DataType="{x:Type my:ClassA}">
       <TextBlock Text="{Binding Path=PropertyA}" />
    </DataTemplate>
    
    <DataTemplate DataType="{x:Type my:ClassB}">
      <TextBlock Text="{Binding Path=PropertyB}" />
    </DataTemplate>

    And in the right side of window define <ContentControl /> element binded to SelectedItem of TreeView:

    <ContentControl Grid.Column="1" 
                    Content="{Binding ElementName=treeView, Path=SelectedValue}" />

    I hope, that I helped you.

    • Marked as answer by sqlguy Sunday, January 06, 2013 6:39 PM
    Sunday, January 06, 2013 4:55 PM
  • Thanks so much.  That worked great.  Spent about 3 days and read more things that did not work.  Yours is simple and works.

    LS


    Lloyd Sheen

    Sunday, January 06, 2013 6:39 PM
  • I am pleasure that I helped you.
    Monday, January 07, 2013 6:47 AM
  • Hi Lloyd,

      You didn't fully understand my answer.  I was trying to get you to the point you didn't have to type anything in! Mino give an example the the DataTemplate, but you have to type manually all the binding as shown.  In my solution you wouldn't have to type in anything!

    Yes it starts with either a datagrid or a deatils view, in the details view all of the elements are created automatically with the proper binding.  You simply copy those elements and dump them into you datatemplate!

    Total time = 10 minutes.


    JP Cowboy Coders Unite!

    Monday, January 07, 2013 2:26 PM
  • The presentation is not just a grid.  The details can contain both "property" information and datagird type information.  I will end up with a set of dictionaries that has the XAML to define the look of the data, so I have what I need.  I have no problem with typing (LOL). 

    Thanks to both,

    LS


    Lloyd Sheen

    Monday, January 07, 2013 2:45 PM
  • Ok sorry you rejected the great advise above.

    JP Cowboy Coders Unite!

    Monday, January 07, 2013 5:09 PM