This is a short post on customizing height and width of the items in GridView. by default if we use a WrapGrid we get items as bunch of tiles.
We can change the height and width of the tiles by deriving from gridview and overriding PrepareContainerForItemOverride method like below. In the code I am making the tile double the width if the city is “City 4”. Ofcourse we can add properties and not hardcode stuff like shown
public class MyGridView : GridView
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
var obj = item as Person;
var gi = element as GridViewItem;
if (obj.City == “City 4”)
{
gi.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, 2);
gi.Background = new SolidColorBrush(Colors.Beige);
gi.Foreground = new SolidColorBrush(Colors.Black);
gi.FontSize = 22;
}
base.PrepareContainerForItemOverride(gi, item);
}
}
Here is the xaml and code I used in the BlankPage .
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Random r = new Random();
var items = Enumerable.Range(0, 50).Select(x =>
new Person { Id = x,
Name = “person..” + x.ToString(),
City = “City ” + (r.Next(0,5)).ToString() });
cvs.Source = items;
}
<Page.Resources> <CollectionViewSource x:Name=”cvs”/> </Page.Resources>
<local:MyGridView FontSize=”20″ FontFamily=”segoe ui” Margin=”100″ ItemsSource=”{Binding Source={StaticResource cvs}}” >
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment=”Left” >
<TextBlock Text=”{Binding Id}” />
<TextBlock Text=”{Binding Name}” />
<TextBlock Text=”{Binding City}” />
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid ItemHeight=”120″ ItemWidth=”120″ MaximumRowsOrColumns=”6″ Orientation=”Vertical”/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</local:MyGridView>
Here is the result.
Thanks friend, this is very simple and easy to understand . I’ve one query, i.e. I want to display records row wise in gridview by default it display column wise.
You can change the orientation to horizontal, in the sample it is vertical.
Do you have a XAML or C# example ? I’m currently trying to change my standard GridView into having a large first tile for each GridView.
No, I dont, the code in the post, should be enough to get started. Are you running into any issues
Hi I dont have the code for VariableSizedWrapGrid. Can you help with that.
Sorry I forgot to mention that I am trying to achieve this in Windows 7 using WPF. Your example matched my requirement perfectly only issues I am facing is code for VariableSizedWrapGrid.
VariableSizeWrapGrid is part of Windows 8 metro not WPF.
I have tried the same it works fine with items but when I try to add group header it UI really goes for a toss 😦 Is there any sample which will show with group header, example : Create a existing GridView Template and implement Variable tile?
How can you make each item 100% the height of the grid view so you have rows of tall items?? I can’t figure this out for the life of me:(
I did not quite understand, do you have a mockup of what it looks like?
Do you know of a way to only allow certain items to be selected in the GridView? I want the GridView to allow multiple selected items but I want to choose and limit what may be actually selected.
you could set IsHitTestVisible on gridviewitem to false to prevent selection
Yea, that would do the trick, thanks…but I actually want to be able to respond to a Tap/Click so turning the HitTest off won’t entirely do.
I’m thinking I may have to go with an entirely new approach to my problem…thanks for your help and quick response!
when i press and hold item on gridview. have to error. what happen
I am not sure I understood, Do you want an error to occur when they Tap and Hpld?
It really write simply, but very usefully. This sample help me to resolve my project problem. Thanks.
This may sound very trivial, but I have been trying increase the distance between the two items. I do not need variable size items but just space between the items to increase. If I use Margin, it just adds margin to a cell which becomes visible when you select ie selection rect is on the complete cell including the margin.
You should change the template of GridViewItem.