节能以下MVVM模式一个WPF画布图像 [英] Saving a WPF canvas as an image following MVVM Pattern

查看:617
本文介绍了节能以下MVVM模式一个WPF画布图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个画布,例如类似该解决方案或许多他人使用的ItemsControl

I have a canvas, e.g. similar to this solution or many others using the ItemsControl.

现在我想这应该绑定到一个ICommand的按钮。该命令应调用视图模型类的方法,可以保存图像。
的保存方法是明确的,但我怎么做MVVM模式下的结合?

Now I want a button which should be bound to an ICommand. This command should call a method of ViewModel class which can save the image. The saving method is clear, but how do I do the binding following the MVVM pattern?

推荐答案

您可以传递画布使用CommandParameter视图模型的保存方法。

You could pass the Canvas to the ViewModel's Save method using a CommandParameter

<Button Content="Save" 
        Command="{Binding SaveCanvasCommand}" 
        CommandParameter="{Binding ElenementName=myCanvas}" ?>

<Canvas x:Name="myCanvas">
   <!-- Stuff to save -->
</Canvas>

和地方在你的ViewModel或命令你必须

And somewhere in you ViewModel or Command you'd have

void SaveCanvasCommandExecute(object parameter)
{
    UIElement toSave = (UIElement)parameter;
    //.. You'd probably use RenderTargetBitmap here to save toSave.
}

这篇关于节能以下MVVM模式一个WPF画布图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆