【问题标题】:Changing Image Source property when Button is clicked [duplicate]单击按钮时更改图像源属性[重复]
【发布时间】:2016-08-14 12:19:48
【问题描述】:

我在我的 Xaml 编辑器中添加了一个图像(image1),将源属性更改为 Source="/WpfApplication9;component/Images/a.png"

我有一个按钮, 我想要的是,如果我单击该按钮,我希望我的 image1 更改其来源。 我已经使用了这段代码,但是当我单击按钮时,具有“a.png”的 image1 什么也没有,或者没有显示任何内容。

这是我的代码

 private void button1_Click(object sender, RoutedEventArgs e)
 {
    image1.Source = (System.Windows.Media.ImageSource)this.Resources["/Resources/a.png"]; 
 }

这是我的 xaml

<Image Height="150" HorizontalAlignment="Left" Margin="153,56,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" Source="/WpfApplication9;component/Images/a.png" />

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    这可能是您可以尝试的,使用BitmapImage 类动态分配新的图像源。

    private void button1_Click(object sender, RoutedEventArgs e)
     {
        image1.Source = new BitmapImage(new Uri("/Resources/a.png", UriKind.RelativeOrAbsolute));
     }
    

    【讨论】:

    • 同理,我觉得只是清除了我图片的source属性
    • 现在可以了,感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多