But it is pain as it has bunch of C# code to write. Is there any better way to achieve this??
Hello meetarun007
your code looks nice already. I don't fully understand what exactly you mean with better way, so here are three different approaches:
I need to create a array of images
interpreting that as 1 dimensional array and as targeting less code to write, it is possible to use a flow view which reduces the original 20 lines of code to 14:
void flowSample()
{
const double fixedImageWidth = 25;
int numberOfRows = Int32.Parse(txtRows.Text);
int numberOfColumns = Int32.Parse(txtColums.Text);
wrapPanel1.Children.Clear();
wrapPanel1.Width = fixedImageWidth * numberOfColumns;
BitmapImage logo = new BitmapImage(new Uri(@"g:\tmp\codeprojectavatar.jpg"));
for (int i = 0; i < numberOfRows*numberOfColumns; i++)
{
Image finalImage = new Image();
finalImage.Width = fixedImageWidth;
finalImage.Source = logo;
finalImage.ToolTip = string.Format("({0},{1})", i % numberOfColumns + 1, i / numberOfColumns + 1);
wrapPanel1.Children.Add(finalImage);
}
}
a grid is also possible. Although there's more code to write for that case, it reflects the two dimensioness more clearly:
void gridSample()
{
const double fixedImageWidth = 25;
int numberOfRows = Int32.Parse(txtRows.Text);
int numberOfColumns = Int32.Parse(txtColums.Text);
grid1.RowDefinitions.Clear();
grid1.ColumnDefinitions.Clear();
for (int r = 1; r <= numberOfRows; r++)
{
RowDefinition row = new RowDefinition();
row.Height = GridLength.Auto;
grid1.RowDefinitions.Add(row);
}
for (int c = 1; c <= numberOfColumns; c++)
{
ColumnDefinition col = new ColumnDefinition();
col.Width = GridLength.Auto;
grid1.ColumnDefinitions.Add(col);
}
BitmapImage logo = new BitmapImage(new Uri(@"g:\tmp\codeprojectavatar.jpg"));
for (int r = 1; r <= numberOfRows; r++)
for (int c = 1; c <= numberOfColumns; c++)
{
Image finalImage = new Image();
finalImage.Width = fixedImageWidth;
finalImage.Source = logo;
finalImage.ToolTip = string.Format("({0},{1})", r, c);
Grid.SetRow(finalImage, r - 1);
Grid.SetColumn(finalImage, c - 1);
grid1.Children.Add(finalImage);
}
}
it can also be approached a little more generic so it can be reused more easily
abstract class dim
{
public int nof;
public abstract void Init();
public Action<UIElement, int, int> setPosition;
}
class dim<T> : dim where T : DefinitionBase, new()
{
public IList<T> defs;
public Action<T, GridLength> setSpacing;
public override void Init()
{
defs.Clear();
for (int i = 1; i <= nof; i++)
{
T d = new T();
setSpacing(d, GridLength.Auto);
defs.Add(d);
}
}
}
void gridSample2()
{
const double fixedImageWidth = 25;
List<dim> dims = new List<dim> {
new dim<RowDefinition> { nof = Int32.Parse(txtRows.Text), defs = grid2.RowDefinitions, setSpacing = new Action<RowDefinition, GridLength>((d, s) => d.Height = s), setPosition = new Action<UIElement, int, int>((e, d0, d1) => Grid.SetRow(e, d0)) },
new dim<ColumnDefinition> { nof = Int32.Parse(txtColums.Text), defs = grid2.ColumnDefinitions, setSpacing = new Action<ColumnDefinition, GridLength>((d, s) => d.Width = s), setPosition = new Action<UIElement, int, int>((e, d0, d1) => Grid.SetColumn(e, d1)) } };
dims.ForEach(d => d.Init());
BitmapImage logo = new BitmapImage(new Uri(@"g:\tmp\codeprojectavatar.jpg"));
Enumerable.Range(0, dims[0].nof).ToList().ForEach(d0 => Enumerable.Range(0, dims[1].nof).ToList().ForEach(d1 =>
{
Image finalImage = new Image();
finalImage.Width = fixedImageWidth;
finalImage.Source = logo;
finalImage.ToolTip = string.Format("({0},{1})", d0+1, d1+1);
dims.ForEach(d => d.setPosition(finalImage, d0, d1));
grid2.Children.Add(finalImage);
}));
}