wpf WPF如何强制设计器显示自定义窗口样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19452291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF how to force designer to display custom window style
提问by Ron
I made a new CustomControlbased on the Window Control.
When I use my Control it doesn't appear in the designer mode, instead it still uses the default window style.
How can I force the designer to display my window style instead of the default one?
我CustomControl基于 Window做了一个新的Control。
当我使用我的控件时,它不会出现在设计器模式中,而是仍然使用默认的窗口样式。
如何强制设计器显示我的窗口样式而不是默认样式?
My MainWindow.xaml:
我的 MainWindow.xaml:
<CustomWindow:MetroWindow x:Class="Testz.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CustomWindow="clr-namespace:MetroWindow;assembly=MetroWindow"
Title="MainWindow" Height="350" Width="525" BorderBrush="Red">
<Grid>
</Grid>
</CustomWindow:MetroWindow>
Link to my whole project - maybe you'll need it
How it looks in the designer and how it really looks:
它在设计器中的外观以及它的实际外观:


采纳答案by Luiz Felipe
I think I understood what you was trying to accomplish.
我想我明白你想要完成什么。
The problem is that the Visual Studio Designer can't find the Resource because it is on the library. What you need to do is to create a ResourceDictionary pointing to it on you Application to be able to see the designer time template.
问题是 Visual Studio 设计器找不到资源,因为它在库中。您需要做的是在您的应用程序上创建一个指向它的 ResourceDictionary,以便能够看到设计器时间模板。
<Application x:Class="DemoMetroWindow.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MetroWindow"
StartupUri="DemoWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:/MetroWindow;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
You can learn more from links bellow.
您可以从下面的链接中了解更多信息。
OnApplyTemplate() never being called
http://blogs.msdn.com/b/jgalasyn/archive/2007/10/29/troubleshooting-wpf-designer-load-failures.aspx
http://blogs.msdn.com/b/jgalasyn/archive/2007/10/29/troubleshooting-wpf-designer-load-failures.aspx
http://blogs.msdn.com/b/jnak/archive/2007/11/08/code-behind-and-the-wpf-designer.aspx
http://blogs.msdn.com/b/jnak/archive/2007/11/08/code-behind-and-the-wpf-designer.aspx
回答by aerojun
You're using Mahapps Metro, right?
您正在使用 Mahapps Metro,对吗?
You can use the styles provided by it. Styling a window with Metro
您可以使用它提供的样式。 使用 Metro 设计窗口样式
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
You can change the color of the window by changing the Resource dictionary of Blue.xaml by other colors, just check it out.
可以通过将Blue.xaml的Resource字典改成其他颜色来改变窗口的颜色,看看就行了。
回答by Martin.Martinsson
When the resource references in App.xaml are fine you should restart Visual Studio. In most cases the themes are then displayed correctly.
当 App.xaml 中的资源引用正常时,您应该重新启动 Visual Studio。在大多数情况下,主题会正确显示。
Regards
问候

