Click here to Skip to main content
15,500,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my Program Ive got and Side Panel which is always display and on the left beside the Panel is space for the Pages which I display when I press the the button on the Side Panel of the Page I want to see.

But now Ive got a button which should Print the Page which is selected.

But for some reason the program didnt print what I have written in the RichTextBox, to understand I have an Richtextbox placeholder which looks like that:

<Grid Grid.Row="1" >
    <RichTextBox x:Name="rtb0" IsHitTestVisible="True"
             TextChanged="OnDocumentChanged" BorderThickness="0"
             Background="Transparent" Panel.ZIndex="2">
        <FlowDocument x:Name="fdoc" TextAlignment="Center"/>

        <RichTextBox.Style>
            <Style TargetType="RichTextBox">
                <Setter Property="FontSize" Value="60"/>
            </Style>
        </RichTextBox.Style>
    </RichTextBox>
    <TextBox x:Name="tb0" Style="{StaticResource tb0}" Text="{Binding TextBox1, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                      TextWrapping="Wrap" Panel.ZIndex="1" TextAlignment="Center"
                     Foreground="DarkGray">

    </TextBox>
    <!--<Grid.Style>
        <Style TargetType="Grid">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding TextBox1}" Value=""/>
                        <Condition Binding="{Binding printcontrol}" Value="True"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Visibility" Value="Hidden"/>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Style>-->
</Grid>


and my Style for that:

XML
<Style TargetType="{x:Type TextBox}" x:Key="tb0">
          <Setter Property="BorderThickness" Value="0"/>
          <Setter Property="FontSize" Value="40"/>
          <Style.Triggers>
              <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Page}}, Path=IsDocumentEmpty0}"
                               Value="False">
                  <Setter Property="Visibility" Value="Hidden"/>

              </DataTrigger>

          </Style.Triggers>
      </Style>


and my OnDocumentChange:
C#
  public bool IsDocumentEmpty0
        {
            get { return (bool)GetValue(IsDocumentEmptyProperty0); }
            set { SetValue(IsDocumentEmptyProperty0, value); }
        }
public static readonly DependencyProperty IsDocumentEmptyProperty0 =
          DependencyProperty.Register("IsDocumentEmpty0", typeof(bool), typeof(A4Page), new PropertyMetadata(false));

 private void OnDocumentChanged(object sender, TextChangedEventArgs e)
        {
            if (sender is RichTextBox richText)
            {
                if (richText.Name == "rtb0")
                {
                    int size = rtb0.Document.ContentStart.GetOffsetToPosition(rtb0.Document.ContentEnd);
                    IsDocumentEmpty0 = (size == 0 // The document has not blocks. Example, after Ctrl+A & Delete. 
                            || size == 2 // The document has 1 block without inlines. For example, 1 paragraph without inline(s).  
                            || size == 4); // The document has 1 block and 1 paragraph with empty inline. 
                }
               
            }
        }

Thats all for that I have an Placholder for an RichTextBox. Now when I print I want to hide all Placeholder that not be used. And thats working ( for that I have the printcontrol Property) but now when I want to print I always get the Page with the placeholder text and not with the text I wrote.


What I have tried:

For that I tried this on my MainWindow:
C#
var vm = (ViewModel)this.DataContext;
           A4Page page = new A4Page();
           page.print();


And that On my Page the Print Method:

C#
var vm = (ViewModel)this.DataContext;
            vm.printcontrol = true;
            PrintDialog printDlg = new PrintDialog();
            int i = 0;
            LocalPrintServer printServer = new LocalPrintServer();

            PrintQueueCollection printQueuesOnLocalServer = printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });


            foreach (PrintQueue printer in printQueuesOnLocalServer)

            {
                printDlg.PrintQueue = new PrintQueue(new PrintServer(), printer.Name);
                //break;
                //if (printer.Name == "Kyocera ECOSYS P3045dn (2)")
                i++;
                if(i == 3)
                    break;
            }
            // printDlg.PrintQueue = new PrintQueue(new PrintServer(),vm.selected_printer);
            printDlg.PrintTicket.CopyCount = vm.PrintCount;

          

            printDlg.PrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4); ///Setzt die Richtige Größe
            printDlg.PrintTicket.PageOrientation = PageOrientation.Portrait;///Setzt das Document auf Vertical Drucken

            printDlg.PrintVisual(PageA4, "Page Printing.");
            vm.printcontrol = false;

But For Some reason its always empty the Richtextbox when I press the print button on my Sidepanel.

I tried to get the text manuel from my RichTextBox with that:

C#
string richText = new TextRange(rtb0.Document.ContentStart, rtb0.Document.ContentEnd).Text;


but for some reason Its always empty
Posted
Updated 5-Aug-22 6:38am

1 solution

Your "TextBox" is sitting in the same "cell" as the "RichTextBox"; you let them both default to the same row and column. The TextBox will cover the RichTextBox (when it has content and / or is stretched).

If that's the plan, you need to hide the TextBox when you want to show what's underneath.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900