将源属性分配给图像控件 WPF 时出现问题
我正在处理一个 WPF C# 项目,并且有多个 (25) 个图像控件排列在表中(5 列,5 行)。每个图像控件称为“图像[行][列]”(例如:Image15)。 为控件分配不同的源只能解决一个问题。无论我使用哪个控件(Image11、Image12、Image 55),它都会影响 Image11。无论我尝试更改哪一个,我最终都会更改第一个(Image11)。这是源代码更改代码:
BitmapImage src3 = new BitmapImage();
src3.BeginInit();
src3.UriSource = new Uri(@"D:\Electricity\CONSUMER_ON.jpg");
src3.EndInit();
Image15.Source = src3;
这确实更改了图像,但就像我编写了“Image11.Source = src3;”一样。下面是 XAML 代码,以防万一它可能与之相关。
<Image Height="150" HorizontalAlignment="Left" Margin="11,10,0,0" Name="Image11" Stretch="Fill" VerticalAlignment="Top" Width="150" />
... 23 more lines removed ...
<Image Height="150" HorizontalAlignment="Left" Margin="635,634,0,0" Name="Image55" Stretch="Fill" VerticalAlignment="Top" Width="150" />
如果您需要任何其他信息,请询问。图像为 200x200、JPEG 格式,并且显示正确(在错误的位置)。控件的图像在运行时确定并加载。
I have a WPF C# project that I am working on and I have multiple (25) Image controls arranged in table (5 columns, 5 rows). Each Image control is called "Image[row][column]" (eg:Image15).
Assigning a different source to the control works fine with ony one problem. No matter which control I use (Image11, Image12, Image 55) it affects Image11. No matter which one I try to change I'll end up changing the first one (Image11). This is the source change code:
BitmapImage src3 = new BitmapImage();
src3.BeginInit();
src3.UriSource = new Uri(@"D:\Electricity\CONSUMER_ON.jpg");
src3.EndInit();
Image15.Source = src3;
This does change the image but acts as if I had written "Image11.Source = src3;". Here's the XAML code just in case it might have anything to do with it.
<Image Height="150" HorizontalAlignment="Left" Margin="11,10,0,0" Name="Image11" Stretch="Fill" VerticalAlignment="Top" Width="150" />
... 23 more lines removed ...
<Image Height="150" HorizontalAlignment="Left" Margin="635,634,0,0" Name="Image55" Stretch="Fill" VerticalAlignment="Top" Width="150" />
if you need any other info, please ask. The images are 200x200 and in JPEG format and are displayed correctly (in the wrong place). The Images for the controls are determined and loaded at run-time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否记得为每个图像控件创建一个新的 BitmapImage,或者您是否回收了同一对象,但在图像之间移动时更改了其内容?如果将两个
Image
控件的源设置为相同的ImageSource
,然后更改该图像,则两个控件都会反映更改。Are you remembering to create a new
BitmapImage
for each image control, or are you recycling the same object but changing its contents when you move between images? If you set the source of twoImage
controls to the sameImageSource
and then alter that image, both controls will reflect the changes.