WPF - 如何以编程方式更改按钮的图像

WPF - How to change button's image programmatically

我正在创建一个 wpf 应用程序并使用 aforge 为我的项目实现 usb 网络摄像头。在我的 CameraWindow 中,我有一个 Image 控件,我在其中显示从我的 usb 网络摄像头捕获的最新图像。 Here is the sample

在我的第三个 window (AllCapturedImages) 我有一个 Image 控件并且在里面 我正在显示从 USB 网络摄像头捕获的最新图像。这发生在我的 CameraWindow.

中使用 Action()
<Image x:Name="newlyAddedImage" Margin="10,10,230,10"/>

public Action<BitmapImage> newlyCapturedImage;
if (newlyCapturedImage != null)
{
    newlyCapturedImage(CapturedImages.Last());
}

在同一个window (AllCapturedImages)我还有三个Image控件 这是其中之一:

<Button x:Name="PreviewButton1" Margin="577,10,25,404">
    <Button.Template>
        <ControlTemplate>
            <Image x:Name="SPPreviewImage1"></Image>
        </ControlTemplate>
    </Button.Template>
</Button>

而我想要的是 change/update 这些 ImageSource 每次我拍摄图像时。

如果您这样定义 ButtonControlTemplate

<Button x:Name="PreviewButton1" Margin="577,10,25,404">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <ContentPresenter />
        </ControlTemplate>
    </Button.Template>
</Button>

...您可以简单地将其 Content 属性 设置为 Image 元素:

var bitmapImage = CapturedImages.Last();
PreviewButton1.Content = new Image() { Source = bitmapImage };