0

I have just started to learn the MVVM methodolgy. I have written a simple application that basically is a application form. My UserControl window is a bunch of labels and textboxes. I bind the textboxes to elements in an Application class. The user can either choose to load the data from a Google spreadsheet or hard code the data. This all works great. I also want to be able to print the form with the values in the textboxes. The window layout prints just fine but the value in the textboxes. I am just doing a printobjext.printvisual command. My question is how do I print the contents of the textboxes, do I need to bind the data in some other way?

Here is part of my UserControl XAML window. just eliminating redundent code.

<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>

Here is part of my Application class in my ApplicationModel.

namespace ApplicationForm.Model

{ public class 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");
            }
        }
    }
}

}

And here is my print routine in 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);
        }
    }
Cass
  • 537
  • 1
  • 7
  • 24
  • 1
    I don't know about printing, but having variable names like `name2` and `name2Birthday` should be causing you to see giant red sirens indicating you probably need a bit of refactoring. – BradleyDotNET Mar 31 '17 at 00:12
  • Also, you don't need to specify the datacontext for *every control*, in fact, given that you are trying to bind the DataContext, its probably failing the bindings. I cant tell since you didn't post all the code but you may want to read through http://stackoverflow.com/questions/23713898/setting-datacontext-in-xaml-in-wpf/23714054 Generally you want to let controls inherit the window's/user control's DataContext – BradleyDotNET Mar 31 '17 at 00:15
  • @BradleyDotNet -- First why would variables like name2 and name2birthday be an issues? Second if I remove the DataContext bindings the fields do not get populated. I have this in my Application.xaml.cs file public ApplicationView() { InitializeComponent(); DataContext = new ViewModel.ApplicationViewModel(); } – Cass Mar 31 '17 at 00:33
  • Does adding a line for "view.grdAppFromGrid.UpdateLayout()" after the view.grdAppFromGrid.Arrange() line have any affect? – Dan Mar 31 '17 at 00:44
  • @Dan -- No it doesn't – Cass Mar 31 '17 at 00:46
  • @Cass - Do the textbox values print ok if you hard-code one of them with static text? – Dan Mar 31 '17 at 00:48
  • You don't need to explicitly set Mode=TwoWay on all those bindings. Bindings on those properties will default to that. And all or most of those x:Name attributes are unused so they're just cluttering up the place. – 15ee8f99-57ff-4f92-890c-b56153 Mar 31 '17 at 02:04

0 Answers0