将源属性分配给图像控件 WPF 时出现问题

发布于 2024-09-11 13:30:34 字数 870 浏览 2 评论 0原文

我正在处理一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

(り薆情海 2024-09-18 13:30:34

您是否记得为每个图像控件创建一个新的 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 two Image controls to the same ImageSource and then alter that image, both controls will reflect the changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文