1

I want to create a Grid dynamically to show product information like any e-commerce site.

I am fetching product information from JSON and then want to show each product details to tiles like view.

3
  • DataGrid ? or a single Object with the product details, placed properly in a Grid ? Commented Jul 7, 2015 at 13:19
  • I thought of creating New object for every product or is there any way to fill datagrid from JSON data source? Commented Jul 7, 2015 at 13:38
  • I'm a bit unclear about what your desired layout is. Can you draw a quick mockup of it in something like balsamiq to describe further? Also can you clarify if you need selection behavior, column headers, or editing templates? If you don't need all or some of those features, an alternate control with a custom template may be a better solution. Commented Jul 7, 2015 at 15:29

1 Answer 1

1

Instead of using loads of code-behind code, my suggestion is using an ItemsControl with a WrapPanel inside.

<ItemsControl ItemsSource="{Binding Products}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!-- Your item template -->
            <Grid Height="120" Width="70">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="3*" />
                    <RowDefinition Height="2*" />
                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0" FontWeight="Bold" Text="{Binding ProductName}" />
                <Image Grid.Row="1" Source="{Binding Thumbnail}" />
                <TextBlock Grid.Row="2" Text="{Binding Description}" />
            </Grid>
            <!-- Your item template -->
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Products would be a collection of items in your DataContext (ViewModel, if you're using MVVM), and you just have to bind to the properties of those items in the Item Template (like ProductName, Thumbnail or Description in my example).

Pros:

  • Less code
  • 100% XAML
  • Adapts itself to the available space
  • Can be easily configured for horizontal or vertical, or left-to-right or right-to-left layouts

Cons:

  • No selection
  • No headers
  • No editable templates
  • Mmmh, it's not a Grid?

If you want all the fancy functionalities, like headers, selection, etc., you'll have to use something like a DataGrid, but then you lose the tiled layout.

Sign up to request clarification or add additional context in comments.

2 Comments

I would also recommend an ItemsControl, however it should be noted it doesn't support the same functionality as a DataGrid such as Selection behavior, Column Headers, Editing Templates, etc. If those features are not needed, I would definitely use an ItemsControl
Yup, you're right. But the OP was asking for a Grid, and since he also mentioned " e-commerce sites", I guessed he just wanted to show items in a tiled layout. I'll add those cons to my answer, though :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.