根据用户输入创建图像阵列 [英] Creating array of images based on user inputs
问题描述
我需要根据用户输入创建一个图像数组。行和没有。 of colums
例如,如果用户给出2x3,则应显示,
ImageAt1x1 ImageAt1x2 ImageAt1x3
ImageAt2x1 ImageAt2x2 ImageAt23
参考图片: http://s7.postimage.org/oypg8660b/stov.png [ ^ ]
现在我以编程方式创建按钮并设置图像属性。
但它很痛苦它有很多C#代码可以写。
有没有更好的方法来实现这个?
注意:鼠标悬停在图像数组中的每个项目上它必须显示它的数字在数组中,如2x1。
I need to create a array of images based on user inputs for no. of rows and no. of colums
For example if user gives 2x3, is should show,
ImageAt1x1 ImageAt1x2 ImageAt1x3
ImageAt2x1 ImageAt2x2 ImageAt23
Ref Image: http://s7.postimage.org/oypg8660b/stov.png[^]
Now i am creating buttons programatically and setting the image property.
But it is pain as it has bunch of C# code to write.
Is there any better way to achieve this??
Note: On mouse hover on each item in image array it has to show its number in array like 2x1.
推荐答案
但它很痛苦,因为它有大量的C#代码可以编写。有没有更好的方法来实现这个目标?
Hello meetarun007
您的代码已经很好了。我不完全明白你用更好的方式究竟是什么意思,所以这里有三种不同的方法:
我需要创建一个图像数组
将其解释为一维数组并将目标编码为较少的代码,可以使用流视图将原始的20行代码减少到14:
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);
}
}
它也可以更接近通用,因此可以更轻松地重复使用< br $> b $ b
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);
}));
}
这篇关于根据用户输入创建图像阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!