locked
How to populate a GridView using inline GridViewItems to test DataTemplate?

    Question

  • Hello,

    Frequently I find myself making mock prototypes of C# WinRT apps and need to quickly populate a GridView with data. I will have already designed the ItemTemplate for the GridViewItem within the GridView item's Datatemplate (including the binding value names). Since this is a quick mockup I don't have the backend built out. Right now the only two ways I know of quickly populating the GridView for my needs are:

    1) paste the GridViewItemTemplate into each GridViewItem and change the values from Binding Values to the real values:

    <GridView >
    <GridView.ItemTemplate>
      <DataTemplate>
        <StackPanel>
          <TextBlock Text="{Binding Name}" Margin="0 0 0 5"/>
          <TextBlock Text="{Binding Address}" Margin="0 0 0 5"/>                                        
        </StackPanel>
      </DataTemplate>
    </GridView.ItemTemplate>
                       
    <GridViewItem>
      <StackPanel>
        <TextBlock Text="Simon Smith" Margin="0 0 0 5"/>
        <TextBlock Text="125 Maple Avenue" Margin="0 0 0 5"/>                                        
      </StackPanel>
    </GridViewItem>
    
    <GridViewItem>
      <StackPanel>
        <TextBlock Text="Janet Janks" Margin="0 0 0 5"/>
        <TextBlock Text="56 Brooks St." Margin="0 0 0 5"/>                                        
      </StackPanel>
    </GridViewItem>
    
    </GridView>


    2: Define a list of objects in the code behind with all the binding properties defined and set this list as the GridView's item source (this is a bit too time consuming for my mockup needs)

    Is there a way I can populate the GridView with GridViewItems specifying the binding values as item properties?

    ideally I was hoping for something quick like this:

    <GridViewItem Name="Mr. Smith" Address="123 Maple Avenue" />
    <GridViewItem Name="Mrs. Janks" Address="56 Brooks St." />
    <GridViewItem Name="Mr. Holden" Address="9005 Central Ave"/>


    What is the quickest way to handle this? Is this something where I could define my own GridViewItems and add my own dependency properties?

    Thanks!




    • Edited by twubneh Thursday, February 27, 2014 11:45 PM typo
    Thursday, February 27, 2014 11:44 PM

Answers

  • Hi twubneh,

    That would be a good suggestion, but I don't think it is possible by now.

    The only way to use Name/Address attribute in GridViewItem is to attach an dependency property, which might be much more complicate than just use binding.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, February 28, 2014 6:33 AM
    Moderator