本文介绍了从视图模型打印 WPF 视觉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用 RelayCommand 的情况下调用打印方法从 ViewModel 打印视觉或窗口,如下例所示:使用 MVVM 模式打印 WPF 视觉效果?

how do i print a visual or window from the ViewModel by just calling a method to print without using a RelayCommand as shown in this example: Print WPF Visuals with MVVM pattern?

var printDlg = new PrintDialog();
printDlg.PrintVisual(View, "JOB PRINTING")

推荐答案

好的,这是一个快速、肮脏但可重用的附加行为类.免责声明:我只是在几分钟内破解了它,它有缺陷,应该很容易克服.

Ok this is a quick and dirty but reusable attached behavior class.Disclaimer: I just hacked this in a couple of minutes and it has its flaws, which should be easy to overcome.

首先我们需要我们的服务类.

First we need our service class.

public class VisualPrinter
{
    private static RoutedUICommand PrintVisualCommand = new RoutedUICommand("PrintVisualCommand", "PrintVisualCommand", typeof(VisualPrinter));

    #region ab PrintCommand

    public static ICommand GetPrintCommand(DependencyObject aTarget)
    {
        return (ICommand)aTarget.GetValue(PrintCommandProperty);
    }

    public static void SetPrintCommand(DependencyObject aTarget, ICommand aValue)
    {
        aTarget.SetValue(PrintCommandProperty, aValue);
    }

    public static readonly DependencyProperty PrintCommandProperty =
        DependencyProperty.RegisterAttached("PrintCommand", typeof(ICommand), typeof(VisualPrinter), new FrameworkPropertyMetadata(PrintVisualCommand));

    #endregion

    #region ab EnablePrintCommand

    public static bool GetEnablePrintCommand(DependencyObject aTarget)
    {
        return (bool)aTarget.GetValue(EnablePrintCommandProperty);
    }
    public static void SetEnablePrintCommand(DependencyObject aTarget, bool aValue)
    {
        aTarget.SetValue(EnablePrintCommandProperty, aValue);
    }

    public static readonly DependencyProperty EnablePrintCommandProperty =
        DependencyProperty.RegisterAttached("EnablePrintCommand", typeof(bool), typeof(VisualPrinter), new FrameworkPropertyMetadata(false, OnEnablePrintCommandChanged));

    private static void OnEnablePrintCommandChanged(DependencyObject aDependencyObject, DependencyPropertyChangedEventArgs aArgs)
    {
        var newValue = (bool)aArgs.NewValue;
        var element = aDependencyObject as UIElement;
        if(newValue)
        {
            element.CommandBindings.Add(new CommandBinding(PrintVisualCommand, OnPrintVisual));
        }
    }

    private static void OnPrintVisual(object aSender, ExecutedRoutedEventArgs aE)
    {
        // the element is the one were you set the EnablePrintCommand
        var element = aSender as Visual;
        var printDlg = new PrintDialog();
        // do the printing
    }

    #endregion
}

这里我们定义了两个附加属性.PrintCommand 用于将实际打印命令注入视图模型,这必须通过 OneWayToSource 完成.第二个,EnablePrintCommand,是启用打印,设置应该打印哪个元素并注册命令绑定.

Here we define two attached properties. PrintCommand is used to inject the actual print command into a view model, this must be done with OneWayToSource.The second, EnablePrintCommand, is to enable the printing, setting which element should be printed and to register the command binding.

我当前的版本有一个缺陷,你不能在同一个元素上设置两个属性,但这很容易被克服.所以要打印一个按钮,它看起来像这样:

My current version has a flaw, that you can't set both properties on the same element, but this can easily be overcome. So to print a a button, it would look like this:

<Grid local:VisualPrinter.EnablePrintCommand="True">
    <Button Content="Test" local:VisualPrinter.PrintCommand="{Binding ViewModelPrint, Mode=OneWayToSource">
</Grid>

虽然 ViewModelPrint 是视图模型中 ICommand 类型的属性,可以执行该属性然后打印网格(因此是按钮).

While ViewModelPrint is a property of type ICommand in the view model, which can be executed and will then print the grid(therefore the button).

这篇关于从视图模型打印 WPF 视觉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:25