Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
Can I choose a field layout based on data Type?

Well, I already know I can do it by defining two different field layouts and giving them names like so:

<igWPF:FieldLayout x:Name="Cars">

  <igWPF:Field Name="MAKE" Label="Make" />

  <igWPF:Field Name="MODEL" Label="Model" />
  <igWPF:Field Name="FuelEconomy" Label="Mileage" />


<igWPF:FieldLayout x:Name="Trucks">
  <igWPF:Field Name="BRAND" Label="Brand" />
  <igWPF:Field Name="MODEL" Label="Model" />

  <igWPF:Field Name="GroundClearance" Label="Ground Clearance" />


I was able to choose a field layout in code behind by handling the AssigningFieldLayoutToItem event. Basically I just look at the type of the bound Item and then apply the named filed layout with:

e.FieldLayout = Cars


e.FieldLayout = Trucks.

However, to fit an MVVM pattern a little better I think  it would be much better to have a DataTemplateSelector kind of like ListBox has. So what would be nice is a declarative way to select a FieldLayout based on type. Is this possible?

  • 30945
    Offline posted

    Hello Louis,


    Thank you for your post. I have been looking into your question and by default, a FieldLayout is assigned to a record, based on the match between the properties of the DataItem of the record and the Fields  in the FieldLayout.Fields collection. This means, that when you have a record, that holds a Car object with Model and Make properties and you have two FieldLayouts in the XamDataGrid: one with Model and Make fields and one with Brand and Model and the Car class does not have a Brand property, the XamDataGrid will choose the first FieldLayout for this record, since the fields of the layout are matching to the properties of the item of the record. You can read more details on FieldLayouts here:


    If, in your scenario, the Truck and the Car classes both have Model, Brand and Make properties and you have two FieldLayouts: one with Model and Make fields and the other with Brand and Model field, the XamDataGrid will choose the first FieldLayout by its index in the FieldLayouts collection of the XamDataGrid, and will assign it to the Record, regardless of its item, since both FieldLayouts has matching field names with the properties of the item of the record. In this scenario, if you wish to be able to set the one FieldLayout to be used for Car and the other for Truck, I can suggest using the Key property of the FieldLayout. When setting the FieldLayout’s Key property to the type of the object, this FieldLayout will be used for the  type that the Key is specifying. For example, if you have the following FieldLayouts:




                    <igWPF:FieldLayout Key="Car">

                        <igWPF:Field Name="Make" />

                        <igWPF:Field Name="Model" />



                    <igWPF:FieldLayout Key="Truck">

                        <igWPF:Field Name="Brand" />

                        <igWPF:Field Name="Model" />





    And you have Car and Truck classes with the same properties: Model, Make, Brand and you set the DataSource of the XamDataGrid to a collection of Trucks, the second FieldLayout will be used, since its key is “Truck”, as the name of the class used in the data source. If the FieldLayouts have not Key set, the first FieldLayout will be used for both Cars and Trucks. I have created a sample application that demonstrates this functionality.


    Please let me know if you need any further assistance on the matter.




    Developer Support Engineer