locked
How to add ListBox inside RichTextBlock for Printing RRS feed

  • Question

  • I need to print document to a printer.

    My document looks like this ; I split it into parts;

    -- part(1)

     Company Name:                                Date:

     Address:

    ---Part(2)

     ItemCode         Name           Price        LineAmount

      ----                 -----             ---           ---

    --part(3)

                                                              Total :--

    Part(1) Using TextBlocks for company Name, Addr, and Date

    Part(2) data is contained in a ListBox

    Part(3) Using textBlock for Total

    Questions :

    Understand I need to use a Xaml Page for part(1,2,3)  as a formatted Page for printing.

    1) How to place the ListBox into a RichTextBlock?

    Thanks.

    Wednesday, November 13, 2013 7:24 AM

Answers

  • I would suggest to give the screen page and the print page the same datacontext. That's also what the printing framework in the reference does. Both screen page and print page use the same data bindings in xaml, but the screen page can do this within regular controls while the print page is restricted to rich textblocks.

    So you fetch the data only once, and store it in the datacontext of the screen page (in MVVM that would be the ViewModel), and when you create the print page, you write something like:

    page1.DataContext = this.DataContext;

     
    • Marked as answer by FireDance Tuesday, November 19, 2013 6:36 AM
    Wednesday, November 13, 2013 9:07 AM

All replies

  • Your XAML would look like this:

    <RichTextBlock>
    <Paragraph>
    <InlineUIContainer>
    <ItemsControl ItemsSource="{Binding YourItems}">
    <ItemsControl.ItemTemplate>
    <DataTemplate> 
    <!-- Data Template -->
    </DataTemplate>
    </ItemsControl.ItemTemplate>
    </ItemsControl>
    </InlineUIContainer>
    </Paragraph>
    </RichTextBlock>
    There's a full example over here.
    Wednesday, November 13, 2013 7:51 AM
  • Hi Diederik Krols,

    Thank u so much for the info. I don't have much printing experience in WinRT. Hope you can help me further.

    To print a document or Invoice, I need the following :

    1)  PagePrinting.xaml  

    2)  FormattedPage.xaml  which contains the Data such as Part(1),(2) and (3) and  data come from a SQLite Database.  FormattedPage.xaml will use your RichTextBlock solution (above)

    My problem:

    To print the document or Invoice base on the FormattedPage.xaml in PagePrinting.xaml, I need to do this.

    FrameworkElement page1;

     page1 = new FormattedPage();

     CanavsPrintContainer.Children.Add(page1);

    Problem:

    1) How to pass the CustomerId to FormattedPage's Constructor in PagePrinting.xaml when Pressing Print Button?

    This CustomerId will be used to get data from SQLite tables in FormattedPage.xaml

    2) If (1) is successful, will data be passed back to PagePrinting.xaml? or any suggestion how to pass back the data?

    Thanks

    Wednesday, November 13, 2013 8:40 AM
  • I would suggest to give the screen page and the print page the same datacontext. That's also what the printing framework in the reference does. Both screen page and print page use the same data bindings in xaml, but the screen page can do this within regular controls while the print page is restricted to rich textblocks.

    So you fetch the data only once, and store it in the datacontext of the screen page (in MVVM that would be the ViewModel), and when you create the print page, you write something like:

    page1.DataContext = this.DataContext;

     
    • Marked as answer by FireDance Tuesday, November 19, 2013 6:36 AM
    Wednesday, November 13, 2013 9:07 AM
  • I need further help:

       PagePrinting.xaml    <---> FormattedPage.xaml


    --1--- in FormattedPage.xaml , I use your solution :

    <RichTextBlock>
     <Paragraph>
      <InlineUIContainer>
       <ItemsControl ItemsSource="{Binding YourItems}">
         <ItemsControl.ItemTemplate>
            <DataTemplate>
              <!-- Data Template -->
            </DataTemplate>
         </ItemsControl.ItemTemplate>
     </ItemsControl>
    </InlineUIContainer>
    </Paragraph>
    </RichTextBlock>


    ------2--  In PagePrinting.xaml :

    I have the data from SQLite Db for your Suggested XAML in (1)

    ObservableCollection<Transaction> TransCollection = new ObservableCollection<Transaction>();


    OnPageLoad :

    TransCollection.Clear();

    var db = new SQLiteAsyncConnection(dbPath);
    var Trans = await db.QueryAsync<BizTransaction>("Select * From BizTransaction Where SalesId = '" + Id + "'");        

    int intRecord = Trans.Count();

    if (intRecord != 0)
     {
       foreach (var _trans in Trans)
       {
          int Id = _trans.Id;
          string Name = _trans.Description;
          string ItemNo = _trans.No;
          int Qty = (int)_trans.Quantity;
          decimal Price = _trans.UnitPrice;
          decimal LineAmt = _trans.LineAmount;

          //-- create a collection
         AddToList(TransId, Name, ItemNo, Qty, Price, LineAmt);

       }


    this.DataContext = TransCollection;

     FrameworkElement page1;
     page1 = new FormattedPage();
     page1.DataContext = this.DataContext;
     CanvasPrintContainer.Children.Add(page1);

     

    I have the followig questions :

    1) is this correct ? this.DataContext = TransCollection;

    2) How to do Binding in FormattedPage.xaml?

    3) What need to be done in xaml section for PagePrinting ?


    Thanks

     

     

    Friday, November 15, 2013 3:19 AM
  • I don't get any result showing in RichTextBlock even there is data.

     this.DataContext = TransCollection;

     FrameworkElement page1;
     page1 = new FormattedPage();
     page1.DataContext = this.DataContext;
     CanvasPrintContainer.Children.Add(page1);

    1) Is this DataBinding correct in RichTextBlock?

    <RichTextBlock HorizontalAlignment="Left" Height="361" Margin="90,188,0,0" VerticalAlignment="Top" Width="1160">
                <Paragraph>            
                    <InlineUIContainer>
                        <ItemsControl ItemsSource="{Binding}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid Width="670" Background="White">

                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>

                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="80"></ColumnDefinition>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                            <ColumnDefinition Width="*"></ColumnDefinition>

                                        </Grid.ColumnDefinitions>

                                        <TextBlock Margin="1,3,0,0" FontSize="23" FontWeight="SemiBold" Foreground="DarkBlue" Text="{Binding No}" Grid.Row="0" Grid.Column="0" />
                                        <TextBlock Margin="3,3,0,0" FontSize="23" Foreground="DarkBlue" Text="{Binding Description}" TextWrapping="Wrap" Grid.Row="0"  Grid.Column="1" />
                                        <TextBlock Margin="30,3,0,0" FontSize="23" Foreground="DarkBlue" Text="{Binding UnitPrice}" TextAlignment="Center"  TextWrapping="Wrap"  Grid.Row="0" Grid.Column="2" />
                                        <TextBlock Margin="50,3,0,0" FontSize="23" Foreground="DarkBlue" Text="{Binding Quantity}" TextAlignment="Center" TextWrapping="Wrap"  Grid.Row="0" Grid.Column="3" />
                                        <TextBlock Margin="3,3,0,0" FontSize="23" Foreground="DarkBlue" Text="{Binding LineAmount}" TextAlignment="Center" TextWrapping="Wrap"  Grid.Row="0" Grid.Column="4" />


                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </InlineUIContainer>
                </Paragraph>
            </RichTextBlock>

    Thanks

    Monday, November 18, 2013 3:35 AM