Question: I am new to WPF and i am not able to figure out this. How do i create a grid with x rows and y columns and insert image into each cell at run time ?
Scenario: I have an inventory project where the end user will search for a item and the application will specify where he can find the item. Now i am already retrieving the cabinet details where the items are kept which again have a rack of x rows and y columns . The rack being displayed can have different row or column sizes.See Image.
Approach: So what i figured out is that i should have a grid of x rows and y columns (known only at run time). Fill an image for each cell value. Insert a different image into the location of the item or highlight that cell value. But i am not able to find how to do that. Most of my searches point to add rows and columns dynamically. Am i missing something very obvious?
Expected Output: Given below is how i would like it to be displayed to my end user :
The Grid class in WPF represents a Grid control. The following code snippet creates a Grid control, sets its width, horizontal alignment, vertical alignment, show grid lines, and background color. Grid DynamicGrid = new Grid();
First Method: By typing XAML Code RowDefinitions property and a ColumnDefinition Element for each column inside the Grid. ColumnDefinitions property. By default, GridLines are invisible. For showing the GridLines, set the ShowGridLines property of the Grid to True.
A Grid is a control for laying out other controls on the form (or page). A DataGrid is a control for displaying tabular data as read from a database for example.
The post demonstrates Wpf data grid with dynamically defined number of rows and columns but all cells has the same size. The post is devoted to the Wpf data grid with dynamically defined number of rows and columns but all cells has the same width and the same height. For example, such grid could be used in chess or checkers games for 8x8 field.
The Grid class in WPF represents a Grid control. The following code snippet creates a Grid control, sets its width, horizontal alignment, vertical alignment, show grid lines, and background color.
DynamicGrid.RowDefinitions.Add (gridRow3); Once rows and columns are added to Grid, you can add any contents to Grid cells by using SetRow and SetColumn methods. SetRow and SetColumn methods take first parameter as the control name and second parameter as row number and column number respectively.
Once a control is created and its position within Grid is set, next step is to add control to Grid by using Grid.Children.Add method. This code snippet adds a TextBlock to Grid. DynamicGrid.Children.Add(txtBlock1); The complete code is listed below.
Like i said i was always wondering whether we need to go for a data grid in cases where we have static data. So this is what i came up with. I create a stack panel and add y images. Now i add x such stack panels. If the exact location of the image comes up i add a highlighted image. worked perfectly. Sharing code for any one who wants to reuse it.
private void CreateGraphicalDisplay(int rows,int columns,int row,int column)
{
ClearGraphicPanel();
for (int i = 1; i <= rows; i++)
{
StackPanel childstack = new StackPanel();
for (int j = 1; j <= columns; j++)
{
Image gage = new Image();
gage.Stretch = Stretch.Fill;
if (i == row && j == column)
{
gage.Source = new BitmapImage(new Uri(@Highlightedimage));
}
else
{
gage.Source = new BitmapImage(new Uri(@NormalImage));
}
gage.Width = 12;
gage.Height =12;
gage.HorizontalAlignment = HorizontalAlignment.Left;
gage.Margin = new Thickness(10, 1, 1, 1);
childstack.Children.Add(gage);
}
containerstack.Children.Add(childstack);
}
}
you can try come thing like this
Xmal
<Grid>
<ItemsControl ItemsSource="{Binding A}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Label Content="{Binding Heading}" BorderThickness="2, 0, 2, 2" BorderBrush="Black"/>
<ItemsControl ItemsSource="{Binding Values}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding}" BorderThickness="0, 0, 2, 2" BorderBrush="Black"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
View Model
class ViewModel
{
public List<Inventory> A
{
get;
set;
}
public ViewModel()
{
A = new List<Inventory>();
for (int i = 1; i <10; i++)
{
Inventory iv = new Inventory();
iv.Heading = "R" + i ;
iv.Values = new List<string>();
for (int j = 0; j < 5; j++)
{
iv.Values.Add("Pic");
}
A.Add(iv);
}
}
}
public class Inventory
{
public string Heading
{
get;
set;
}
public List<string> Values
{
get;
set;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With