代码之家  ›  专栏  ›  技术社区  ›  Ron

WPF如何强制设计器显示自定义窗口样式

  •  10
  • Ron  · 技术社区  · 11 年前

    我做了一个新的 CustomControl 基于Window Control .
    当我使用控件时,它不会出现在设计器模式中,而是仍然使用默认的窗口样式。
    如何强制设计器显示我的窗口样式而不是默认的窗口样式?

    我的主窗口.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

    它在设计师身上的外观和实际外观:

    enter image description here

    3 回复  |  直到 11 年前
        1
  •  6
  •   Community c0D3l0g1c    7 年前

    我想我理解你想要实现的目标。

    问题是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>
    

    您可以从下面的链接中了解更多信息。

    OnApplyTemplate() never being called

    WPF get Type in Design time?

    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

        2
  •  0
  •   Community c0D3l0g1c    4 年前

    你在使用马哈普斯地铁,对吧?

    您可以使用它提供的样式。 Styling a window with 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>
    

    您可以通过将Blue.xaml的资源字典更改为其他颜色来更改窗口的颜色,只需检查即可。

        3
  •  0
  •   Martin.Martinsson    8 年前

    当App.xaml中的资源引用正常时,您应该重新启动Visual Studio。在大多数情况下,主题会正确显示。

    当做