删除ContentDialog

时间:2017-03-05 03:52:04

标签: uwp uwp-xaml

我正在编写一个ContentDialog,它是UWP应用程序的一部分,我不需要默认的PrimaryButton和SecondaryButton。如果我没有定义按钮,对话框底部会有一个厚填充。请参阅随附的屏幕截图。有没有办法删除空间?谢谢。

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以为此对话框创建一个新样式,删除包含按钮空间的Grid。打开文件file,搜索ContentDialog,复制应用中的默认样式,为其指定x:Key并修改它:

<Style TargetType="ContentDialog" x:Key="NoButtonsContentDialogStyle">
    <Setter Property="Foreground" Value="{ThemeResource ContentDialogForeground}" />
    <Setter Property="Background" Value="{ThemeResource ContentDialogBackground}" />
    <Setter Property="BorderBrush" Value="{ThemeResource ContentDialogBorderBrush}" />
    <Setter Property="HorizontalAlignment" Value="Center" />
    <Setter Property="VerticalAlignment" Value="Top" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="MaxHeight" Value="{ThemeResource ContentDialogMaxHeight}" />
    <Setter Property="MinHeight" Value="{ThemeResource ContentDialogMinHeight}" />
    <Setter Property="MaxWidth" Value="{ThemeResource ContentDialogMaxWidth}" />
    <Setter Property="MinWidth" Value="{ThemeResource ContentDialogMinWidth}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentDialog">
                <Border x:Name="Container">
                    <Grid x:Name="LayoutRoot">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Border x:Name="BackgroundElement"
                            Background="{TemplateBinding Background}"
                            FlowDirection="{TemplateBinding FlowDirection}"
                            BorderThickness="{ThemeResource ContentDialogBorderWidth}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            MaxWidth="{TemplateBinding MaxWidth}"
                            MaxHeight="{TemplateBinding MaxHeight}"
                            MinWidth="{TemplateBinding MinWidth}"
                            MinHeight="{TemplateBinding MinHeight}">
                            <Grid x:Name="DialogSpace" VerticalAlignment="Stretch" Padding="{ThemeResource ContentDialogPadding}">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <ScrollViewer x:Name="ContentScrollViewer"
                                    HorizontalScrollBarVisibility="Disabled"
                                    VerticalScrollBarVisibility="Disabled"
                                    ZoomMode="Disabled"
                                    Margin="{ThemeResource ContentDialogContentScrollViewerMargin}"
                                IsTabStop="False">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="*" />
                                        </Grid.RowDefinitions>
                                        <ContentControl x:Name="Title"
                                            Margin="{ThemeResource ContentDialogTitleMargin}"
                                            Content="{TemplateBinding Title}"
                                            ContentTemplate="{TemplateBinding TitleTemplate}"
                                            FontSize="20"
                                            FontFamily="XamlAutoFontFamily"
                                            FontWeight="Normal"
                                            Foreground="{TemplateBinding Foreground}"
                                            HorizontalAlignment="Left"
                                            VerticalAlignment="Top"
                                            IsTabStop="False">
                                            <ContentControl.Template>
                                                <ControlTemplate TargetType="ContentControl">
                                                    <ContentPresenter Content="{TemplateBinding Content}"
                                                        MaxLines="2"
                                                        TextWrapping="Wrap"
                                                        ContentTemplate="{TemplateBinding ContentTemplate}"
                                                        Margin="{TemplateBinding Padding}"
                                                        ContentTransitions="{TemplateBinding ContentTransitions}"
                                                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                                </ControlTemplate>
                                            </ContentControl.Template>
                                        </ContentControl>
                                        <ContentPresenter x:Name="Content"
                                            ContentTemplate="{TemplateBinding ContentTemplate}"
                                            Content="{TemplateBinding Content}"
                                            FontSize="{ThemeResource ControlContentThemeFontSize}"
                                            FontFamily="{ThemeResource ContentControlThemeFontFamily}"
                                            Margin="{ThemeResource ContentDialogContentMargin}"
                                            Foreground="{TemplateBinding Foreground}"
                                            Grid.Row="1"
                                            TextWrapping="Wrap" />
                                    </Grid>
                                </ScrollViewer>
                                <!-- I've removed the button content here -->
                            </Grid>
                        </Border>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

添加此代码,例如您全局加载的App.xaml或样式文件,在创建ContentDialog时,在代码中设置样式:

ContentDialog dialog = new ContentDialog();
dialog.Style = Application.Current.Resources["NoButtonsContentDialogStyle"] as Style;