wpf - 如何更改 MahApps.Metro 对话框内容模板宽度?

标签 wpf xaml modal-dialog controltemplate mahapps.metro

我想更改 MahApps.Metro 对话框的基本模板(或创建新的对话框类型),因为我想在狭窄的登录窗口中显示它们。现在,消息中几乎所有第二个单词都在新行中,但右侧和左侧有很大的空格,我想减少这些空格。

Too wide padding on the sides

我在BaseMetroDialog.xaml中找到了消息对话框垂直分为三部分:左侧 25% 空间,内容 50% 空间,以及左侧 25% 空间右边。我想更改这些数字。

但是我怎样才能改变BaseMetroWindow的控件模板呢?和我的新的一起吗?

最佳答案

只需创建您自己的样式来覆盖对话框Template(并添加DialogShownStoryboard)。

<Style TargetType="{x:Type Dialog:BaseMetroDialog}"
        x:Key="NewCustomDialogStyle"
        BasedOn="{StaticResource {x:Type Dialog:BaseMetroDialog}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Dialog:BaseMetroDialog}">
                <ControlTemplate.Resources>
                    <Storyboard x:Key="DialogShownStoryboard">
                        <DoubleAnimation AccelerationRatio=".9"
                                            BeginTime="0:0:0"
                                            Duration="0:0:0.2"
                                            Storyboard.TargetProperty="Opacity"
                                            To="1" />
                    </Storyboard>
                </ControlTemplate.Resources>
                <Grid Background="{TemplateBinding Background}">
                    <Border FocusVisualStyle="{x:Null}"
                            Focusable="False">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <ContentPresenter Grid.Row="0"
                                                Content="{TemplateBinding DialogTop}" />
                            <Grid Grid.Row="1">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="10*" />
                                    <ColumnDefinition Width="80*" />
                                    <ColumnDefinition Width="10*" />
                                </Grid.ColumnDefinitions>
                                <!--  Content area  -->
                                <Grid Grid.Column="1"
                                        Margin="0 10 0 0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <TextBlock Grid.Row="0"
                                                FontSize="{DynamicResource DialogTitleFontSize}"
                                                Foreground="{TemplateBinding Foreground}"
                                                Text="{TemplateBinding Title}"
                                                TextWrapping="Wrap" />
                                    <ContentPresenter Grid.Row="1"
                                                        Content="{TemplateBinding Content}" />
                                </Grid>
                            </Grid>
                            <ContentPresenter Grid.Row="2"
                                                Content="{TemplateBinding DialogBottom}" />
                        </Grid>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="Loaded">
                        <EventTrigger.Actions>
                            <BeginStoryboard Storyboard="{StaticResource DialogShownStoryboard}" />
                        </EventTrigger.Actions>
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这里的命名空间是

xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"

现在使用这个自定义样式,例如对于自定义对话框

<Dialog:CustomDialog x:Key="CustomDialogTest"
                        Style="{StaticResource NewCustomDialogStyle}"
                        Title="This dialog allows arbitrary content. It will close in 5 seconds."
                        x:Name="CustomTestDialog">
    <StackPanel>
        <TextBlock Height="30"
                    Text="This dialog allows arbitrary content. You have to close it yourself by clicking the close button below."
                    TextWrapping="Wrap"
                    Foreground="{DynamicResource AccentColorBrush}" />
        <Button Content="Close Me!" />
    </StackPanel>
</Dialog:CustomDialog>

主要演示的屏幕截图

enter image description here

更新

使用最新版本的 MahApps.Metro 现在可以更改,例如全局 MessageDialog 样式。

<Style TargetType="{x:Type Dialog:MessageDialog}"
       x:Key="NewCustomMessageDialogStyle"
       BasedOn="{StaticResource {x:Type Dialog:BaseMetroDialog}}">
  <Setter Property="Template">
    <!-- the custom template for e.g. MessageDialog -->
  </Setter>
</Style>

<Style TargetType="{x:Type Dialog:MessageDialog}" BasedOn="{StaticResource NewCustomMessageDialogStyle}" />

enter image description here

希望有帮助!

关于wpf - 如何更改 MahApps.Metro 对话框内容模板宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30751663/

相关文章:

jquery - 用jQuery淡化&lt;iframe&gt;的音量

wpf - 边框宽度显示不一致

wpf - 如何在 WPF 中读取用户的自定义键盘快捷键?

windows - 如何更改针对 Windows Phone 8.1 (WinRT) 更改的文本的 TextBlock 前景色?

wpf - 是否可以使用样式或模板应用混合行为?

javascript - 如何防止模态框关闭?

c# - WPF FlowDocument - 绝对字符位置

c# - WPF ListView GridView 单元格样式和绑定(bind)

wpf - 如何在WPF XAML模板中将文本放入圆圈?

objective-c - 关闭模态窗口时的 EXC_BAD_ACCESS ([NSWindow _restoreLevelAfterRunningModal] : message sent to deallocated instance)