2013-04-08 75 views
0

如何通過調用打印方法來打印視圖或窗口,而不使用如本示例中所示的RelayCommand進行打印:Print WPF Visuals with MVVM pattern從視圖模型打印WPF視覺

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

如果您使用MVVM,爲什麼要讓ViewModel知道您的View? – Dutts 2013-04-08 10:12:54

+0

爲什麼你不想使用命令?那麼你總是可以將視圖綁定到視圖模型中的屬性。我會適當地使用一個附加的行爲。使命令成爲附加屬性,在該元素上,將命令OneWayToSource綁定到視圖模型,在那裏觸發它,並通過附加的行爲在控件上處理它。 – dowhilefor 2013-04-08 10:38:19

+0

我想在特定事件發生後打印文檔/窗口..因此打印沒有用RelayCommand調用,並且事件/條件發生在代碼中..不在view/xaml上 – 2013-04-08 10:46:30

回答

0

好吧,這是一個快速,骯髒但可重複使用的附加行爲類。 免責聲明:我只是在幾分鐘之內就砍掉了它,它有缺陷,應該很容易克服。

首先我們需要我們的服務類。

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,用於啓用打印,設置應打印哪個元素並註冊命令綁定。

我目前的版本有一個缺陷,即你不能在同一個元素上設置兩個屬性,但這很容易被克服。因此,要打印的按鈕,它看起來像這樣:

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

雖然ViewModelPrint是類型的ICommand的視圖中的模型,其可以被執行,並且然後將打印網格(因此的按鈕)的性質。