WPF - 单击按钮时更改按钮图像源

WPF - Change button image source when clicks button

我正在尝试在发生点击事件时更改图像源。我可以更改 XAML 中的图像,但不能通过代码更改。我尝试从按钮获取图像 属性,但这不起作用。

我正在使用 Caliburn 进行事件调用:

<Button Content="Mark1" Height="30" Width="40" 
cal:Message.Attach="[Event Click] = [Action SayHello($source, $this)]">
   <Image Name="Mark1ButtonImage"  Source="image1.png" />
</Button>

在 C# 代码中:

public void SayHello(object sender, object doodlesource)
{
    var selectedButton = sender as Button;
    var selectedKrav = doodlesource as Doodle;
    if (selectedButton != null)
    {
       ///WHAT TO DO TO CHANGE? selectedButton.Image doesn't work?
    }
}

要在按钮内找到您的图片,您需要使用这行代码:

var image = (sender as Button).FindVisualChildren<Image>();

然后你可以更改图片来源。

试试这个。

    Image img = new Image();
    img.Source = new BitmapImage(new Uri(@"foo.png"));

    SelectedBtn.Content=img