2015年08月19日
川俣晶の縁側ソフトウェア技術雑記 total 7713 count

Force ContentDialog of UWP to maximize in window

Written By: 川俣 晶連絡先

Before §

Before Screen Shot

After §

After Screen Shot

Example XAML Source Code §

<ContentDialog

    x:Class="SizableContentDialog.ContentDialog3"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:SizableContentDialog"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    Title="TITLE"

    PrimaryButtonText="Button1"

    SecondaryButtonText="Button2"

    PrimaryButtonClick="ContentDialog_PrimaryButtonClick"

    SecondaryButtonClick="ContentDialog_SecondaryButtonClick">

    <Grid x:Name="MyGrid">

        <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top">Small Text</TextBlock>

        <TextBlock HorizontalAlignment="Right" VerticalAlignment="Bottom">Small Text</TextBlock>

    </Grid>

</ContentDialog>

Source Code (NEW:Replaced in Aug.6,2016) §

using System;

using Windows.UI;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Shapes;

namespace ResizeContentDialogUtil

{

    public static class ResizeContentDialogUtil

    {

        private static T find<T>(DependencyObject root, Func<T, bool> checker = null) where T : DependencyObject

        {

            if (root == null) return null;

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)

            {

                var r = VisualTreeHelper.GetChild(root, i);

                var r1 = r as T;

                if (r1 != null)

                {

                    if (checker == null) return r1;

                    if (checker(r1)) return r1;

                }

                var r2 = find<T>(r, checker);

                if (r2 != null) return r2;

            }

            return null;

        }

        public static void FixContentDialogForConstructor(ContentDialog dialog)

        {

            dialog.MaxHeight = double.PositiveInfinity;

            dialog.MaxWidth = double.PositiveInfinity;

            dialog.Loaded += (sender, e) =>

            {

                Grid mainGrid = (Grid)dialog.Content;

                var largeRect = new Rectangle();

                largeRect.Width = 30000;

                largeRect.Height = 30000;

                largeRect.Fill = new SolidColorBrush(Colors.Transparent);

                mainGrid.Children.Insert(0, largeRect);

            };

        }

    }

}

How to use §

 Insert call to FixContentDialogForConstructor method in constructor in a class that inherited from ContentDialog.

This is a example.

public ContentDialog1()

{

    this.InitializeComponent();

    ResizeContentDialogUtil.ResizeContentDialogUtil.FixContentDialogForConstructor(this);

}