Mahapps.metro对话框,如何全局覆盖默认样式?

时间:2018-09-27 15:05:48

标签: wpf dialog override wpf-controls mahapps.metro

最近,我尝试覆盖 mahapps.metro 对话框样式。作为基础,我从here中获取了示例代码。 这是我用来覆盖对话框默认样式的代码。 的确,我不知道如何调用在 UserInputDialog.xaml 中定义为自定义对话框的对话框窗口。 如果有人向我展示如何全局覆盖默认样式,那也很好。 预先感谢!

App.xaml

 <Application x:Class="testMetroDialog_2.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:testMetroDialog_2"
             xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
             xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"            
            StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>

            <ResourceDictionary.MergedDictionaries>
                <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                <!-- Accent and AppTheme setting -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
                <!--Dialog resource dictionary declaration-->
                <ResourceDictionary  Source="DialogResource.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

    </Application.Resources>
</Application>

相应地 DialogResource.xaml

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:testMetroDialog_2"
                        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                        xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro">

     <!-- Override default template for Mahapps custom dialog -->

        <Style TargetType="{x:Type Dialog:MessageDialog}"
           x:Key="NewCustomMessageDialogStyle"
           BasedOn="{StaticResource {x:Type Dialog:BaseMetroDialog}}">
            <Setter Property="Template">

                <!-- Custom template xaml code -->

                <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>

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

    </ResourceDictionary>

UserInputDialog.xaml

  <Dialog:CustomDialog
        x:Class="testMetroDialog_2.UserInputDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:testMetroDialog_2"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="UserInputDialog"
        Width="500"
        Height="200"
        Style="{StaticResource NewCustomMessageDialogStyle}"
        mc:Ignorable="d">




        <StackPanel Margin="5,5,5,5">
            <TextBlock
                Height="40"
                Foreground="{DynamicResource AccentColorBrush}"
                Text="This dialog allows arbitrary content. You have to close it yourself by clicking the close button below."
                TextWrapping="Wrap" />
            <Button Content="Close Me!" />
        </StackPanel>

    </Dialog:CustomDialog>

0 个答案:

没有答案