GridView items spacing code behind, in CreatePrintPreviewPages event handler

BitSmithy 1,951 Reputation points


I am trying to remowe space between GridView items code behind.

I do it when I create print preview pages.

I cooked such code:

        GridView printPageGridView = new GridView();
        List<FrameworkElement> printPreviewPages = new List<FrameworkElement>();

void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
            Style style = new Style();
            style.TargetType = typeof(GridViewItem);
            Setter marginSetter = new Setter();
            marginSetter.Property = GridViewItem.MarginProperty;
            marginSetter.Value = 0;
            printPageGridView.ItemContainerStyle = style;
            Rectangle r1 = new Rectangle();
            r1.Width = 400;
            r1.Height = 200;
            r1.StrokeThickness = 1;
            r1.Stroke = new SolidColorBrush(Colors.Red);
            Rectangle r2 = new Rectangle();
            r2.Width = 400;
            r2.Height = 200;
            r2.StrokeThickness = 1;
            r2.Stroke = new SolidColorBrush(Colors.Red);
            printDocument.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Final);

But it doesnt work. My page in print preview appears with spacing.

I test when print preview I generated first time, and when I changed page orientation and preview is generated next time.

Is it possible to remove space between GridView items in my case?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 16,151 Reputation points Microsoft Vendor

    Hi @BitSmithy ,

    Welcome to Microsoft Q&A!

    According to the document FrameworkElement.Margin,

    Property Value Thickness Provides margin values for the object. The default value is a default Thickness with all properties (dimensions) equal to 0.

    You need to use Thickness to set the value of MarginProperty.

    Setter marginSetter = new Setter();
    marginSetter.Property = GridViewItem.MarginProperty;
    marginSetter.Value = new Thickness(0);

    Thank you.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful