简体   繁体   中英

Grid Binding in Windows phone 7?

i am taking one grid having 3 rows and 3 columns .i want to bind the images to that grid actually images are added to grid but it is not added in correct way.ie they are not place with in cells of grid .

How to bind 12 images correctly to cells of grid .i want all images are having same size and width placing properly in that grid.

 <Grid  HorizontalAlignment="left" Name="grid_main" Visibility="collapsed">
 </Grid>

Here is the code which will arrange the 4 images in one row.

    int j = 0;
    int k = 0;
for (i = 1; i < 13; i++)
         {
           grid_image = new Image();
           ColumnDefinition coldef = new ColumnDefinition();
            coldef.Width = GridLength.Auto;
            grid_main.ColumnDefinitions.Add(coldef);
            // do this for each row
            RowDefinition rowdef = new RowDefinition();
            rowdef.Height = GridLength.Auto;
            grid_main.RowDefinitions.Add(rowdef);
            Grid.SetColumn(grid_image, 0);
            Grid.SetRow(grid_image, 0);

            if (j < 4)
            {
                grid_image.SetValue(Grid.RowProperty, k);
                grid_image.SetValue(Grid.ColumnProperty, j++);
                grid_main.Children.Add(grid_image);
            }
            else
            {
                j = 0; k = k + 1;
                grid_image.SetValue(Grid.RowProperty, k);
                grid_image.SetValue(Grid.ColumnProperty, j++);
                grid_main.Children.Add(grid_image);
            }

        }

I think my version is more clearly (3 col, rows auto):

        ColumnDefinition coldef = new ColumnDefinition();
        coldef.MinWidth = 135;
        gridCat.ColumnDefinitions.Add(coldef);
        coldef = new ColumnDefinition();
        coldef.MinWidth = 135;
        gridCat.ColumnDefinitions.Add(coldef);
        coldef = new ColumnDefinition();
        coldef.MinWidth = 135;
        gridCat.ColumnDefinitions.Add(coldef);

        RowDefinition rowdef = rowdef = new RowDefinition();
        rowdef.MinHeight = 135;
        gridCat.RowDefinitions.Add(rowdef);

        for (int i = 0; i < App.CatViewModel.Items.Count; i++)
        {
            Image grid_image = new Image();
            ImageSourceConverter c = new ImageSourceConverter();
            grid_image.SetValue(Image.SourceProperty,  c.ConvertFromString("img/touro.png"));
            grid_image.SetValue(Image.WidthProperty, 128.0);
            grid_image.SetValue(Image.HeightProperty, 128.0);

            if ((i + 1) % 3 == 0)
            {
                rowdef = new RowDefinition();
                rowdef.MinHeight = 135;
                gridCat.RowDefinitions.Add(rowdef);
            }

            grid_image.SetValue(Grid.RowProperty, i/3);
            grid_image.SetValue(Grid.ColumnProperty, i%3);
            gridCat.Children.Add(grid_image);

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM