Thursday, February 27, 2014

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

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!







No comments:

Post a Comment