【发布时间】:2018-12-11 12:38:51
【问题描述】:
我是编程新手,对我的英语感到抱歉。
我对 MahApps.Metro 样式覆盖有疑问。
将样式添加到对话框控件时,Visual Studio 中的 XAML 设计器为空白(请参阅下面的屏幕截图链接)。
它看起来像一个内容为空的对话框,没有文本和控件。
在自定义对话框中添加样式之前
Dialog1.xaml
<Dialog:CustomDialog x:Class="DesignerTestWithStyle.Dialog1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DesignerTestWithStyle"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
mc:Ignorable="d"
d:DesignHeight="150" d:DesignWidth="250"
>
<Grid>
<StackPanel>
<Button Content="Test"/>
<Label Content="Test text"/>
<TextBox Text="I don't see this text"/>
</StackPanel>
</Grid>
在CustomDialog中添加样式后
Dialog1.xaml
<Dialog:CustomDialog x:Class="DesignerTestWithStyle.Dialog1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DesignerTestWithStyle"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
mc:Ignorable="d"
d:DesignHeight="150" d:DesignWidth="250"
Style="{StaticResource Dialog1Style}"
>
<Grid>
<StackPanel>
<Button Content="Test"/>
<Label Content="Test text"/>
<TextBox Text="I don't see this text"/>
</StackPanel>
</Grid>
其中 Dialog1Style 是在 App.xaml 中声明的 ResourceDictionary 文件,根据此问题的提示创建:
How to change MahApps.Metro dialog content template width?
在我自己的样式中覆盖模板似乎有问题。
Visual Studio 更新没有帮助。
Windows 10 是最新的。
我的 Visual Studio 2017 是社区版 15.9.3,专业版和企业版也出现这个问题。
编辑:
创建新项目并添加 mahapps.metro 包后,我的 app.xaml 文件为空。
其实app.xaml:
<Application x:Class="DesignerTestWithStyle.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DesignerTestWithStyle"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Styles/Dialog1Style.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
和 Dialog1Style.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DesignerTestWithStyle.Styles"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
>
<Style TargetType="{x:Type Dialog:BaseMetroDialog}"
x:Key="Dialog1Style"
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>
【问题讨论】:
-
在 app.xaml 中,在 mahapps 资源定义之后的下一行定义您的样式资源,然后 mah 应用程序将不会覆盖您的样式。如果不行,试试这个,然后也提供你有问题的风格
-
Shubham Sahu - 谢谢回答。 app.xaml 是空的,即使在安装了 mahapps.metro 包之后也是如此。看看我编辑的帖子,我添加了样式文件。
标签: wpf visual-studio xaml designer mahapps.metro