我在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"/>
我想在“选定”功能中获取图像的来源。 我选择的功能如下
private void selected(object sender, MouseButtonEventArgs e)
{
//do somethings....
}
如何将所选图像(发件人)的来源分配给新的Image对象? 类似于以下内容
Image newimage = new Image();
newimage.Source = //something..
有没有办法动态获取源?
答案 0 :(得分:5)
将您的发件人投射为图片,您将能够使用Source属性:
private void selected(object sender, MouseButtonEventArgs e)
{
Image newimage = new Image();
newimage.Source = ((Image)sender).Source;
}
答案 1 :(得分:1)
使用event的OriginalSource属性并将其强制转换为Image:
var clickedImage = (Image)e.OriginalSource;
Image newimage = new Image();
newimage.Source = clickedImage.Source;