Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用MVVM模式打印WPF视觉效果_Mvvm_Printing_Datagrid - Fatal编程技术网

使用MVVM模式打印WPF视觉效果

使用MVVM模式打印WPF视觉效果,mvvm,printing,datagrid,Mvvm,Printing,Datagrid,我的ViewModel有一个PrintCommand,它执行一个名为PrintCalendar()的方法。但是日历(也称为datagrid)在视图中,那么如何将datagrid放入ViewModel中呢 把我的手弄脏然后用代码隐藏所有的东西?哦,不 PrintDialog printDlg = new PrintDialog(); printDlg.PrintVisual(datagrid, "Grid Printing."); 你可以试试这个。我已经用datagrid、buttom和View

我的ViewModel有一个PrintCommand,它执行一个名为PrintCalendar()的方法。但是日历(也称为datagrid)在视图中,那么如何将datagrid放入ViewModel中呢

把我的手弄脏然后用代码隐藏所有的东西?哦,不

PrintDialog printDlg = new PrintDialog();
printDlg.PrintVisual(datagrid, "Grid Printing.");

你可以试试这个。我已经用datagrid、buttom和ViewModel设置了一个简单的演示窗口。ViewModel包含PrintCommand(来自的RelayCommand),该命令接受Visual(datagrid)作为命令参数。代码中没有代码,所有工作都是通过绑定完成的

Xaml:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:WpfTestApplication.ViewModel"
    x:Class="WpfTestApplication.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">
    <Window.Resources>
        <ResourceDictionary>
            <vm:WindowViewModel x:Key="WindowViewModel"/>
        </ResourceDictionary>
    </Window.Resources>

    <Grid x:Name="LayoutRoot" DataContext="{DynamicResource WindowViewModel}">
        <DockPanel>
            <Button Content="Print" Width="70" DockPanel.Dock="Bottom" HorizontalAlignment="Right"
                    Command="{Binding PrintCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=dataGrid, Mode=OneWay}" />
            <DataGrid x:Name="dataGrid" DataContext="{DynamicResource SampleDataSource}" ItemsSource="{Binding Collection}"/>
        </DockPanel>
    </Grid>
</Window>

以及ViewModel:

using System.Windows.Controls;
using System.Windows.Media;
using GalaSoft.MvvmLight.Command;

namespace WpfTestApplication.ViewModel
{
    public class WindowViewModel
    {
        /// <summary>
        /// Command executed to print an visual component. The component is passed in as a parameter.
        /// </summary>
        public RelayCommand<Visual> PrintCommand
        {
            get
            {
                return new RelayCommand<Visual>( v =>
                {
                    PrintDialog printDlg = new PrintDialog();
                    printDlg.PrintVisual( v, "Grid Printing." );
                } );
            }
        }
    }
}
使用System.Windows.Controls;
使用System.Windows.Media;
使用GalaSoft.MvvmLight.Command;
命名空间WpfTestApplication.ViewModel
{
公共类WindowViewModel
{
/// 
///为打印可视组件而执行的命令。该组件作为参数传入。
/// 
公共RelayCommand打印命令
{
得到
{
返回新的RelayCommand(v=>
{
PrintDialog printDlg=新建PrintDialog();
printDlg.PrintVisual(v,“网格打印”);
} );
}
}
}
}

你在跟踪我,是吗?;-)你说的当然是对的。只需将对象传递给ViewModel。我的要求改变了,所以我不得不在codebehind中做很多技术工作,以打印带有datagrid+richtext的页面。你知道如何打印一个由许多控件组成的页面吗?不,没有跟踪你,只是看到你有一个未回答的问题:)你说的在一个页面上是什么意思?您的意思是使用来自UI不同部分的多个控件组合打印输出吗?从技术上讲,这个示例打破了MVVM模式。通常,您希望避免在视图模型中使用WPF对象,如
PrintDialog
。然而,在这种情况下,您需要使用WPF对话框控件并在其上调用一个方法,这并不符合MVVM模式,因此您必须选择在视图模型中的线条外面稍微加一点颜色,或者只使用在代码中保留这类内容