Search code examples
c#gridviewwindows-8windows-runtimewinrt-xaml

How to Display Gridview items with variable width in Windows 8?


My GridView items having the size of it's first item size. How do i can change this behaviour ?

How to display GridView items with variable Width as per the content ?


enter image description here

I want to show the first one but i am getting second one.
Any suggestion to do that?


Solution

  • Here is my solution.

    //variable sized grid view

    public class VariableSizedGridView : GridView
    {
        protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item)
        {
            try
            {
                dynamic gridItem = item;
    
                var typeItem = item as CommonType;
                if (typeItem != null)
                {
                    var heightPecentage = (300.0 / typeItem.WbmImage.PixelHeight);
                    var itemWidth = typeItem.WbmImage.PixelWidth * heightPecentage;
                    var columnSpan = Convert.ToInt32(itemWidth / 10.0);
    
    
                    if (gridItem != null)
                    {
                        element.SetValue(VariableSizedWrapGrid.ItemWidthProperty, itemWidth);
                        element.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, columnSpan);
                        element.SetValue(VariableSizedWrapGrid.RowSpanProperty, 1);
                    }
                }
            }
            catch
            {
                element.SetValue(VariableSizedWrapGrid.ItemWidthProperty, 100);
                element.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, 1);
                element.SetValue(VariableSizedWrapGrid.RowSpanProperty, 1);
            }
            finally
            {
                base.PrepareContainerForItemOverride(element, item);
            }
        }
    

    //Collection View source

         <CollectionViewSource  x:Name="collectionViewSource"
                               Source="{Binding ImageList,Mode=TwoWay}"
                               IsSourceGrouped="False"
                               ItemsPath="Items" />   
    

    //variable sized Grid view xaml

         <controls:VariableSizedGridView x:Name="ImageGridView"
                          AutomationProperties.AutomationId="ImageGridView"                            
                          ItemsSource="{Binding Source={StaticResource collectionViewSource}}" 
                          IsItemClickEnabled="True"
                          TabIndex="1" >
         <controls:VariableSizedGridView.ItemTemplate>
         <DataTemplate>
        <Grid Height="300" >
            <Image Stretch="Uniform" Source="{Binding WbmImage}" />
        </Grid>
        </DataTemplate>
        </controls:VariableSizedGridView.ItemTemplate>
                    <controls:VariableSizedGridView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <VariableSizedWrapGrid  ItemWidth="10"   ItemHeight="300" Orientation="Vertical"/>
                        </ItemsPanelTemplate>
                    </controls:VariableSizedGridView.ItemsPanel>                  
        </controls:VariableSizedGridView>