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:
<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:
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
|| size == 2
|| size == 4);
}
}
}
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:
var vm = (ViewModel)this.DataContext;
A4Page page = new A4Page();
page.print();
And that On my Page the Print Method:
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);
i++;
if(i == 3)
break;
}
printDlg.PrintTicket.CopyCount = vm.PrintCount;
printDlg.PrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4); printDlg.PrintTicket.PageOrientation = PageOrientation.Portrait;
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:
string richText = new TextRange(rtb0.Document.ContentStart, rtb0.Document.ContentEnd).Text;
but for some reason Its always empty