MVVM方法从用户控制窗口中的文本框打印文本内容

时间:2017-03-31 00:10:33

标签: c# wpf mvvm printing

我刚开始学习MVVM方法论。我写了一个简单的应用程序,基本上是一个申请表。我的UserControl窗口是一堆标签和文本框。我将文本框绑定到Application类中的元素。用户可以选择从Google电子表格加载数据或硬编码数据。一切都很好。我还希望能够使用文本框中的值打印表单。窗口布局打印得很好,但文本框中的值。我只是在做一个printobjext.printvisual命令。我的问题是如何打印文本框的内容,是否需要以其他方式绑定数据?

这是我的UserControl XAML窗口的一部分。只是消除了冗余代码。

<UserControl x:Class="ApplicationForm.Views.ApplicationView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ApplicationForm"
    xmlns:model="clr-namespace:ApplicationForm.Model"
    xmlns:views="clr-namespace:ApplicationForm.Views"
    xmlns:viewModel="clr-namespace:ApplicationForm.ViewModel"
    mc:Ignorable="d"
    Height="980" Width="800" Background="#FFDAFDF2">

<!--<UserControl.Resources>
    <ResourceDictionary>
        <viewModel:ApplicationViewModel x:Key="AppViewModel"/>
    </ResourceDictionary>
</UserControl.Resources>-->

<Grid x:Name="grdAppForm">
    <Grid x:Name="grdAppFormGrid" DocumentViewerBase.IsMasterPage="True" VirtualizingPanel.VirtualizationMode="Standard"
         VirtualizingPanel.IsVirtualizingWhenGrouping="True" HorizontalAlignment="Center" VerticalAlignment="Center" Height="820" Width="700">
        <Label x:Name="lblName" Content="Name:" Height="28" Width="58" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,10,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16" />
        <TextBox x:Name="tbxName" DataContext="{Binding Applicants, Mode=TwoWay}" Text="{Binding FullName, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, BindsDirectlyToSource=True}" 
                Height="22" Width="215" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="83,10,0,0" FontFamily="Arial" FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" FontSize="16" VerticalContentAlignment="Bottom"/>
        <Label x:Name="lblFamilySize" Content="Family Size:" Height="28" Width="102" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="362,10,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16" />
        <Label x:Name="lblFamO" Content="O:" Height="28" Width="27" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="461,10,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16"/>
        <TextBox x:Name="txbFamO" DataContext="{Binding Applicants, Mode=TwoWay}" Text="{Binding Path=FamilyO, Mode=TwoWay, BindsDirectlyToSource=True}" Height="22" Width="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="486,10,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
        <Label x:Name="lblFamA" Content="A:" Height="28" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="506,10,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16"/>
        <TextBox x:Name="tbxFamA" DataContext="{Binding Applicants}" Text="{Binding Path=FamilyA, Mode=TwoWay}" Height="22" Width="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="531,10,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
        <Label x:Name="lblFamC" Content="C:" Height="28" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="552,10,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16"/>
        <TextBox x:Name="txtFamC" DataContext="{Binding Applicants}" Text="{Binding Path=FamilyC, Mode=TwoWay}" Height="22" Width="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="577,10,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
        <Label x:Name="lblEqual" Content="=" Height="28" Width="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="601,10,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="16"/>
        <TextBox x:Name="tbxFamTot" DataContext="{Binding Applicants}" Text="{Binding Path=FamilyTotal, 
                Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" Height="22" Width="30" 
                HorizontalAlignment="Left" VerticalAlignment="Top" Margin="624,10,0,0" FontFamily="Arial" 
                FontWeight="Bold" FontSize="16" BorderBrush="Black" BorderThickness="0,0,0,3" IsTabStop="False" VerticalContentAlignment="Bottom"/>
        <Label x:Name="lblRenewalDate" Content="Renewal Date:" Height="28" Width="119" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="127,60,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold"/>
        <TextBox x:Name="tbxRenewalDate" DataContext="{Binding Applicants}" Text="{Binding Path=RenewalDate, Mode=TwoWay}" Height="22" Width="180" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="247,60,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
        <Label x:Name="lblAddressDisp" Content="Address:" Height="28" Width="80" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="80,115,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold"/>
        <TextBox x:Name="tbxAddress1" DataContext="{Binding Applicants, Mode=TwoWay}" Text="{Binding Path=Address1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, BindsDirectlyToSource=True}" Height="22" Width="344" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="162,115,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" TextWrapping="Wrap" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
        <TextBox x:Name="tbxAddress2" DataContext="{Binding Applicants}" Text="{Binding Path=Address2, Mode=TwoWay}" Height="22" Width="426" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="80,145,0,0" FontFamily="Arial" FontSize="16" FontWeight="Bold" TextWrapping="Wrap" BorderThickness="0,0,0,3" BorderBrush="Black" VerticalContentAlignment="Bottom"/>
        </grid>

<Grid x:Name="grdButtonsGrid" HorizontalAlignment="Center" Height="80" Width="700" Margin="20, 880,0,0">
        <Button x:Name="btnLoadApplicant" Command="{Binding LoadApplicantCommand}" 
                HorizontalAlignment="Left" Height="43" Margin="60,25,0,0" VerticalAlignment="Top" 
                Width="78" FontFamily="Arial" FontSize="14.667" Background="#FFDAFDF2" BorderThickness="2">
            <TextBlock Text="Load Applicant" TextWrapping="Wrap" />
        </Button>
        <Button x:Name="btnGetApplicant" Command="{Binding GetApplicantCommand}" 
                HorizontalAlignment="Left" Height="43" Margin="150,25,0,0" VerticalAlignment="Top"
                Width="95" FontFamily="Arial" FontSize="14.667" Background="#FFDAFDF2" BorderThickness="2">
            <TextBlock Text="Get Applicant By Name" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="WrapWithOverflow" />
        </Button>
        <Button x:Name="btnPrintApplicant" Command="{Binding PrintApplicantCommand}" 
                HorizontalAlignment="Left" Height="43" Margin="255,25,0,0" VerticalAlignment="Top" 
                Width="70" FontFamily="Arial" FontSize="14.667" Background="#FFDAFDF2" BorderThickness="2">
            <TextBlock Text="Print Applicant" TextWrapping="Wrap"/>
        </Button>
        <Button x:Name="btnClearData" Command="{Binding ClearDataCommand}" 
                HorizontalAlignment="Left" Height="43" Margin="335,25,0,0" VerticalAlignment="Top" 
                Width="58" FontFamily="Arial" FontSize="14.667" Background="#FFDAFDF2" BorderThickness="2">
            <TextBlock Text="Clear Data" TextWrapping="Wrap"/>
        </Button>
        <Button x:Name="btnClose" Content="Close" Command="{Binding CloseCommand}" 
                HorizontalAlignment="Left" Height="43" Margin="440,25,0,0" VerticalAlignment="Top" 
                Width="58" FontFamily="Arial" FontSize="14.667" Background="#FFDAFDF2" BorderThickness="2">
        </Button>
    </Grid>
</grid>

这是我的ApplicationModel中的Application类的一部分。

namespace ApplicationForm.Model

{     公共类ApplicationModel     {     }

public class Applicant : INotifyPropertyChanged
{
    private string firstName;
    private string lastName;
    private string familyO;
    private string familyA;
    private string familyC;
    private string renewalDate;
    private string address1;
    private string address2;
    private string city;
    private string state;
    private string zip;
    private string phoneNumber;
    private string name1Birthday;
    private string name2;
    private string name2Birthday;
    private string name3;
    private string name3Birthday;
    private string name4;
    private string name4Birthday;
    private string name5;
    private string name5Birthday;
    private string name6;
    private string name6Birthday;
    private string name7;
    private string name7Birthday;
    private string name8;
    private string name8Birthday;


    public string FirstName
    {
        get
        {
            return firstName;
        }

        set
        {
            if (firstName != value)
            {
                firstName = value;
                RaisePropertyChanged("FirstName");
                RaisePropertyChanged("FullName");
            }
        }
    }

    public string LastName
    {
        get { return lastName; }

        set
        {
            if (lastName != value)
            {
                lastName = value;
                RaisePropertyChanged("LastName");
                RaisePropertyChanged("FullName");
            }
        }
    }

    public string FullName
    {
        get
        {
            return firstName +  " " + lastName;
        }
    }

    public string FamilyO
    {
        get { return familyO; }

        set
        {
            if (familyO != value)
            {
                familyO = value;
                RaisePropertyChanged("FamilyO");
            }
        }
    }

    public string FamilyA
    {
        get { return familyA; }

        set
        {
            if (familyA != value)
            {
                familyA = value;
                RaisePropertyChanged("FamilyA");
            }
        }
    }
}

}

这是我在ApplicationViewModel中的打印例程。

public void PrintApplicant(object selectedItem)
    {
        ApplicationView view = new ApplicationView();
        PrintDialog prnt = new PrintDialog();
        ObservableCollection<Applicant> applicants = new ObservableCollection<Applicant>();


        applicants = Applicants;

        if (prnt.ShowDialog() == true)
        {
            Size pageSize = new Size(prnt.PrintableAreaWidth - 30, prnt.PrintableAreaHeight - 30);
            view.grdAppFormGrid.Measure(pageSize);
            view.grdAppFormGrid.Arrange(new Rect(5, 5, pageSize.Width, pageSize.Height));
            prnt.PrintVisual(view.grdAppFormGrid, applicants[0].FullName);
        }
    }

0 个答案:

没有答案