简体   繁体   中英

Adding Grid/FrameworkElement to ContainerVisual

Well, this might be a strange question. But as title says, can you add Grid to ContainerVisual. Since Grid inherits Visual, I should be able to do it via Children.Add.

Why do I need this? Well, I'm using FlowDocument to print a report. This report needs to have a header, and since Flow Document doesn't support Headers, I decided to add headers during pagination, by using a solution found on the internet.

Also since I don't want to draw entire header by hand but be able to edit it during desing in a designer, I designed it in a separate file as a Grid element. So my header looks something (I shortened it) like this:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="2cm" Width="18.7cm">
<Grid.Resources>
    <!-- some resources -->
</Grid.Resources>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="2cm"/>
    <ColumnDefinition/>
    <ColumnDefinition Width="2cm"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition/>
    <RowDefinition/>
</Grid.RowDefinitions>
<Border Grid.RowSpan="2">
    <Image Source="Logo.jpg"/>
</Border>
<Border Grid.Column="1" Grid.RowSpan="2">
    <StackPanel>
        <Label Name="staje" Style="{DynamicResource naslov}"></Label>
        <Label Name="predmet" Style="{DynamicResource naslov}"></Label>
    </StackPanel>
</Border>
<Border Grid.Column="2" BorderThickness="1">
    <StackPanel>
        <TextBlock>Datum:</TextBlock>
        <Label Name="datum">22. 12. 2013.</Label>
    </StackPanel>
</Border>
<Border Grid.Column="2" Grid.Row="1" BorderThickness="1,0,1,1">
    <StackPanel>
        <TextBlock>Strana:</TextBlock>
        <Label Name="strana">
            1/2
        </Label>
    </StackPanel>
</Border>
</Grid>

at each pagination call I load the header using the folowing code:

    public FrameworkElement GetHeaderForPage(int Strana)
    {
        FrameworkElement header = Application.LoadComponent(new Uri("/Header.xaml", UriKind.Relative)) as FrameworkElement;

        Label lblTest = LogicalTreeHelper.FindLogicalNode(header, "staje") as Label;
        Label lblPredmet = LogicalTreeHelper.FindLogicalNode(header, "predmet") as Label;
        Label lblDatum = LogicalTreeHelper.FindLogicalNode(header, "datum") as Label;
        Label lblStrana = LogicalTreeHelper.FindLogicalNode(header, "strana") as Label;

        lblTest.Content = KakavTest;
        lblPredmet.Content = Predmet;
        lblDatum.Content = Datum;
        lblStrana.Content = string.Format("{0}", Strana);

        return header;
    }

And finally in the pagination call I place it in the page like so:

        DocumentPage page = m_Paginator.GetPage(pageNumber);

        // Create a wrapper visual for transformation and add extras
        ContainerVisual newpage = new ContainerVisual();

        FrameworkElement header = headerGen.GetHeaderForPage(pageNumber);
    //    header.RenderTransform = new TranslateTransform(0, -header.ActualHeight+10);

        ContainerVisual smallerPage = new ContainerVisual();
        smallerPage.Children.Add(page.Visual);

        //smallerPage.Transform = new MatrixTransform(0.95, 0, 0, 0.95,
        //    0.025 * page.ContentBox.Width, 0.025 * page.ContentBox.Height);

        newpage.Children.Add(smallerPage);
        newpage.Children.Add(header);


        newpage.Transform = new TranslateTransform(m_Margin.Left, m_Margin.Top);

        RenderTargetBitmap bmp = new RenderTargetBitmap((int)m_PageSize.Width, (int)m_PageSize.Height, 96, 96, PixelFormats.Default);
        bmp.Render(newpage);
        ImageShow show = new ImageShow(bmp);
        show.Show();


        return new DocumentPage(newpage, m_PageSize, Move(page.BleedBox), Move(page.ContentBox));

ImageShow class simply opens up new window with an image representing bmp. I was using it to see if the problem was in further processing that is done to display the pages in DocumentViewer. But since ImageShow doesn't display the header Grid, it seems tht I'm doing something terribly wrong.

IN SHORT: Can you add Grid element to ContainerVisual as a child and have it be drawn correctly. Or do I need to draw it by hand?

In the end I hard coded it all by hand. So no, anything that is higher than VIsual and DrawingVisual, can not be included into ContainerVisual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM