We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Use a single property from the object on an ItemSource as the property to display

If I have the following ViewModel:
public class ObjectWrapperViewModel
{ public object SourceObject { get; set; } //An unknown object, generate the table columns. public bool WasProcessed { get; set; } //Used for styling the data grid row. }

Which is bound to the view with:







The resulting view is (which is what I'd expect):


However, is there a way to point the SfDataGrid to the property "SourceObject" as the main property to display in the grid so the result is displayed like it is below but with the highlighting still.


The result should be:
1) The SfDataGrid generates the columns automaticly for any given object.
2) Should still have access to the "
WasProcessed" property (or others) in the ViewModel.

4 Replies

CA Carl November 16, 2015 10:14 AM UTC

The view code is:

<Syncfusion:SfDataGrid ItemsSource="{Binding Objects}">
    <Syncfusion:SfDataGrid.RowStyle>
        <Style TargetType="Syncfusion:VirtualizingCellsControl">
            <Style.Triggers>
                <DataTrigger Binding="{Binding WasProcessed}" Value="True">
                    <Setter Property="Background" Value="#BCFF77"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding WasProcessed}" Value="False">
                    <Setter Property="Background" Value="#FF7777"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Syncfusion:SfDataGrid.RowStyle>
</Syncfusion:SfDataGrid>


SR Sivakumar R Syncfusion Team November 16, 2015 10:31 AM UTC

Hi Carl,

You can bind inner properties with SfDataGrid by setting MappingName like below, (Complex properties)
<syncfusion:SfDataGrid AutoGenerateColumns="False"
                       ItemsSource="{Binding Objects}">
    <syncfusion:SfDataGrid.Columns>
        <syncfusion:GridTextColumn MappingName="SourceObject.ID" />
        <syncfusion:GridTextColumn MappingName="SourceObject.Name" />
        <syncfusion:GridTextColumn MappingName="SourceObject.Offset" />
    </syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

Refer below UG link,
http://help.syncfusion.com/wpf/sfdatagrid/data-binding#complex-property-binding

It is not possible to autogenerate the columns based on SourceObject as this is inner property. You have to define columns manually as in the above code snippet. You can skip WasProcessed column, by manually defining columns.

Thanks,
Sivakumar


CA Carl November 16, 2015 10:36 AM UTC

Hello Sivakumar,

I don't know if the object will always have the columns ID, Name and Offset though so I can't simply define the columns in the XAML like you have suggested.

I could generate the columns from code behind but was simply wondering if there was a way to specify an underlying property to bind with to the row instead.

Carl


JG Jai Ganesh S Syncfusion Team November 17, 2015 12:37 PM UTC

Hi Carl,

As mentioned in our previous update, you have to specify the columns at runtime to meet your requirement. We don’t have any properties to generate columns based on inner properties of underlying Data.

Regards,
Jai Ganesh S


Loader.
Live Chat Icon For mobile
Up arrow icon