1

I want to display a grid of images. Initially the grid will be empty and then it will gradually fill in as the images arrive from an external device, possibly in random order, i.e., I may get the image for location (1,3) and then (5,5) etc.

The dimensions will be known at runtime, but before I display the grid, so I have to set them programmatically, not in the XAML.

1. The examples I've found on the web show how to define Grid rows and columns in XAML, but how do I do it programmatically in C#, i.e., if a 5x4 Grid is specified I want to the user to see an empty 5x4 Grid initially.

2. How do I programmatically access/alter elements at individual grid locations? When the grid is empty I'll want to initially show something in each empty spot (like an "X" or a colored rectangle) and then replace it with the actual image when it arrives. In C# how do I access Grid location (x,y) to add or change what's in it?

3 Do Grids have any intrinsic scrolling ability or do I have to wrap them in a separate scrolling control, i.e.,

<ScrollViewer>
    <Grid>
    </Grid> 
</ScrollViewer>

... and if I do this can I set it for scrollbars to only appear if they are needed based on the size?

user316117
  • 7,971
  • 20
  • 83
  • 158
  • To not make the scrollbar always show up, remove the scrollviewer. It will work as intended once you remove it. – Taugenichts Aug 14 '14 at 14:33

1 Answers1

2
  1. It's easy, see msdn

        // Create the Grid
        Grid myGrid = new Grid();
        myGrid.Width = 250;
        myGrid.Height = 100;
        myGrid.HorizontalAlignment = HorizontalAlignment.Left;
        myGrid.VerticalAlignment = VerticalAlignment.Top;
        myGrid.ShowGridLines = true;
    
        // Define the Columns
        ColumnDefinition colDef1 = new ColumnDefinition();
        ColumnDefinition colDef2 = new ColumnDefinition();
        ColumnDefinition colDef3 = new ColumnDefinition();
        myGrid.ColumnDefinitions.Add(colDef1);
        myGrid.ColumnDefinitions.Add(colDef2);
        myGrid.ColumnDefinitions.Add(colDef3);
    
        // Define the Rows
        RowDefinition rowDef1 = new RowDefinition();
        RowDefinition rowDef2 = new RowDefinition();
        RowDefinition rowDef3 = new RowDefinition();
        RowDefinition rowDef4 = new RowDefinition();
        myGrid.RowDefinitions.Add(rowDef1);
        myGrid.RowDefinitions.Add(rowDef2);
        myGrid.RowDefinitions.Add(rowDef3);
        myGrid.RowDefinitions.Add(rowDef4);
    
        // Add the first text cell to the Grid
        TextBlock txt1 = new TextBlock();
        txt1.Text = "2005 Products Shipped";
        txt1.FontSize = 20; 
        txt1.FontWeight = FontWeights.Bold;
        Grid.SetColumnSpan(txt1, 3);
        Grid.SetRow(txt1, 0);
    
        ...
        myGrid.Children.Add(txt1);
        mainWindow.Content = myGrid
    
  2. Set grid position, get it (getting is more complicated)

  3. I'd probably use ListBox in your case, with WrapPanel. You will get selection (multiple selection?) and scrolling as a bonus. Search for "wpf listbox wrappanel", to example, here is something.

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • Your Grid suggestions worked great (got the layout and grid lines, etc) **except** for the text. No text appears and also I don't understand how the text is inserted since you don't reference "myGrid" just the general Grid class, so how does it know you mean that particular grid? I tried replacing "Grid" with "myGrid" and got "cannot be accessed with an instance reference; qualify it with a type name instead" – user316117 Aug 14 '14 at 16:16
  • `Grid.SetRow()` is [attached property](http://msdn.microsoft.com/en-us/library/ms749011.aspx). And yes, you are right, my example is not complete, it miss `myGrid.Children.Add(txt1);`, but you can find a complete in the msdn link for first question answer (and some more info). – Sinatr Aug 15 '14 at 07:12