locked
How can I access the ItemsWrapGrid defined in the GridView in C#

    Question

  • I have a GridView defined in XAML.  Here the GridView.ItemsPanel --> ItemsPanelTemplate --> ItemsWrapGrid has "MaximumRowsOrColumns" defined in XAML.

    If I need to change the MaximumRowsOrColumns value programmatically based on current page size, how can I access the ItemsWrapGrid object in C#?  "this.MyItemsWrapGrid" is not working.

                <GridView>
                    ......
                    ......
                    <GridView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <ItemsWrapGrid x:Name="MyItemsWrapGrid" MaximumRowsOrColumns="5"/>
                        </ItemsPanelTemplate>
                    </GridView.ItemsPanel>
                </GridView>
    Thanks!
    Tuesday, April 29, 2014 11:51 PM

Answers

  • In the code behind, have a private var ItemsWrapGrid which you set when Gridview is loaded. 

    XAML

     <GridView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <ItemsWrapGrid Loaded="MyItemsWrapGrid_Loaded" x:Name="MyItemsWrapGrid"
                                       MaximumRowsOrColumns="5" />
                    </ItemsPanelTemplate>
    </GridView.ItemsPanel>

    C#

            private ItemsWrapGrid _itemsWrapGrid;
            private void MyItemsWrapGrid_Loaded(object sender, RoutedEventArgs e)
            {
                _itemsWrapGrid = sender as ItemsWrapGrid;
            }


    • Edited by Bryan Stump Wednesday, April 30, 2014 12:51 AM
    • Marked as answer by LChen_MSDN Wednesday, April 30, 2014 8:19 PM
    Wednesday, April 30, 2014 12:51 AM