由于某些原因,我必须在堆栈面板内操作一些图像(不在XAML中)
<StackPanel x:Name="spImageList"
Orientation="Horizontal"
HorizontalAlignment="Center"
Canvas.ZIndex="1" />
和操作图像的代码:
public void ManipulateArrows()
{
spImageList.Children.Clear();
for (int i=0;i<cout;i++)
{
Image img = new Image();
img.Name = "img" + i.ToString();
img.Source = new BitmapImage(new Uri("/Assets/Image/arrow_blue.png", UriKind.Relative));
spImageList.Children.Add(img);
}
}
现在我需要更改该stackpanel中图像的来源,那我该怎么做呢?
public void ChangeImage(string name)
{
spImageList.Children .. = new BitmapImage(new Uri("/Assets/Image/arrow_red.png", UriKind.Relative));;
// Get the image by the name like "img1","img2"
}
答案 0 :(得分:1)
试试这个:
public void ChangeImages()
{
for (int j = 0; j < spImageList.Children.Count; j++)
{
Image img = (Image)Stack.FindName("img"+j.ToString());
img.Source = new BitmapImage(new Uri("/Assets/Image/arrow_red.png", UriKind.Relative));
}
}
答案 1 :(得分:0)
您可以使用FindName
方法,
Image imag1 = (Image)spImageList.FindName("img1");
Image imag2 = (Image)spImageList.FindName("img2");