简体   繁体   English

用户在c#中单击它时获取图像源

[英]get the source of image when user clicks it in c#

i have included four photos in xaml code as follows 我在xaml代码中包含了四张照片,如下所示

<Image Grid.Column="0" 
       Source="Assets/1.png"
       Name="m1"
       MouseLeftButtonDown="selected"/>
<Image Grid.Column="1" 
       Source="Assets/2.png"
       Name="m2"
       MouseLeftButtonDown="selected"/>
<Image Grid.Column="2" 
       Source="Assets/3.png" 
       Name="m3"
       MouseLeftButtonDown="selected"/>
<Image Grid.Column="3" 
       Source="Assets/4.png" 
       Name="m4"
       MouseLeftButtonDown="selected"/>

i want to get the source of the image in "selected" function. 我想在“选定”功能中获取图像的来源。 my selected function is as follows 我选择的功能如下

private void selected(object sender, MouseButtonEventArgs e)
{
    //do somethings....
}

How can i assign the source of the selected image(sender) to a new Image object?. 如何将所选图像(发送方)的源分配给新的Image对象? something similar to follows 类似的事情如下

Image newimage = new Image();
newimage.Source = //something..

Is there a way to dynamically get the source? 有没有办法动态获取源?

Cast your sender as an image and you will be able to use the Source property: 将您的发件人转换为图片,您将能够使用Source属性:

private void selected(object sender, MouseButtonEventArgs e)
{
    Image newimage = new Image();
    newimage.Source = ((Image)sender).Source;
}

Use OriginalSource property of event and cast it to Image: 使用事件的OriginalSource属性并将其强制转换为Image:

var clickedImage = (Image)e.OriginalSource;
Image newimage = new Image();
newimage.Source = clickedImage.Source;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#:当用户单击按钮时如何查看图像? - C#: How do I view an image when a user clicks on a button? 当用户单击不正确的按钮时,如何使c#应用程序弹出视频? - How do I get my c# application to popup a video when user clicks incorrect button? C#treeView,当用户单击子节点时如何获取父节点 - C# treeView, how do I get the parent nodes when user clicks the child node 如何在C#中从网页源获取图像源 - How to get a image source from a webpage's source in C# C#-捕获用户在其他应用程序上的点击 - C# - Capturing user clicks on other application 用户单击“还原”后,获取Windows Form C#的宽度和高度 - Get Width and Height of Windows Form C# After user clicks on Restore 当用户使用C#在Unity中单击它们时,我希望两个对象播放它们各自的动画 - I would like two objects to play their separate animations when the user clicks on them in Unity using C# 用户单击浏览器停止按钮时如何停止服务器端处理(ASP.net C#) - How to stop server side processing when user clicks browser stop button (ASP.net C#) 当用户使用MVC3 C#asp.net单击按钮时显示弹出对话框 - Display popup dialog box when user clicks button using MVC3 C# asp.net ASP.NET MVC3 C# - 当用户单击按钮时,将DB中的记录从1更改为2,反之亦然 - ASP.NET MVC3 C# - when user clicks button, change record in DB from 1 to 2 and viceversa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM