[英]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.