【发布时间】:2016-03-12 15:56:10
【问题描述】:
我在一个窗口中有 40 张图像需要更改。我认为检查这 40 个图像是否需要在计时器上更改的最简单方法是将它们放入 List<ImgInfo>,其中 ImgInfo 包含 System.Windows.Controls.Image。
我的代码更新了列表,但不会更改窗口上的当前图像。
我对在 WPF 中处理图像非常陌生。 我应该将这些图像绑定到代码中的属性吗?现在 xaml 只是将源设置为单个图像。将这些图像绑定到列表中的项目需要什么?
// xaml:
<Image x:Name="comp01" Height="75" Source="img/comp-offline.png" Stretch="Fill" Margin="5"/>
// When the application starts:
ImgInfoList= new List<ImgInfo>();
ImgInfoList.Add(new ImgInfo{ CurrentImage = comp01, MachineName = "" });
ImgInfoList.Add(new ImgInfo{ CurrentImage = comp02, MachineName = "" });
// ... etc for 40 images
// ImgInfo class:
public class ImgInfo
{
public string MachineName { get; set; }
public Image CurrentImage { get; set; }
}
public void MainTimer_Tick(object sender, EventArgs e)
{
foreach(var imgInfo in ImageInfoList)
{
if(//conditions are true)
{
Image imgTmp = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri(LoadBitmapURIFromResource("img/img-1.png"));
bi3.EndInit();
imgTmp.Stretch = Stretch.Fill;
imgTmp.Source = bi3;
imgInfo.CurrentImage = imgTmp;
}
// else if... img/img-2.png, etc.
}
}
//LoadBitmapURIFromResource just returns Convert.ToString(new Uri(@"pack://application:,,,/" + assembly.GetName().Name + ";component/" + pathInApplication, UriKind.Absolute));
【问题讨论】: