根据当前主题更改控制模板

时间:2013-04-25 12:16:20

标签: wpf themes controltemplate

不幸的是,我不得不覆盖我的应用程序中TabControl之一的控件模板,因为我需要对外观进行一些细微的修改,否则无法做到。

<TabControl Name="specialTabControl" Template="{StaticResource SpecialTabControlTemplate} />". 

在切换操作系统的主题之前,一切看起来都很好。现在我的TabControl看起来完全是垃圾,因为它使用了错误主题的控件模板(我使用控件模板的主题,使用Blend提取)。

所以我想要找到一种方法来为需要根据当前操作系统主题选择的特殊TabControl提供4个控制模板(luna,aero,xp,classic)。

那么如何根据当前主题为specialTabControl提供和应用不同的自定义控件模板,以便当用户切换操作系统的主题时,specialTabControl将切换到我为该主题提供的控件模板?

请注意,我在应用程序中还有其他TabControl s,它们没有覆盖控制模板,并且应始终拥有该主题的标准控件模板。

1 个答案:

答案 0 :(得分:0)

我认为您需要在WPF应用程序中查看主题。程序集可以包含以下行:

[组件:ThemeInfo(ResourceDictionaryLocation的 SourceAssembly 下, ResourceDictionaryLocation.SourceAssembly)]

这意味着应用程序的系统主题位于程序集中(在文件夹/主题中)。主题名称必须符合系统主题...例如:

The Aero theme (Windows Vista and Windows 7): themes\Aero.NormalColor.xaml
The default Windows XP theme: themes\Luna.NormalColor.xaml
The olive green Windows XP theme: themes\Luna.Homestead.xaml
The Windows Classic theme: themes\Classic.xaml

当用户更改系统外观时,WPF应用程序将自动下载您的主题。因此,您可以为每个系统主题设置控制模板。更多信息可在以下网址找到:

“Adam Nathan.WPF 4 Unleashed”。第14章。

主题WPF应用程序: http://blogs.infosupport.com/theming-wpf-applications/

我希望这会有所帮助。

*编辑*

我找到了一个有趣的例子,其中提到了实际改变的主题:

http://northhorizon.net/2010/how-to-actually-change-the-system-theme-in-wpf/

您可以设置显式样式,该样式不会响应系统中的更改皮肤:

<Style x:Key="ExplicitGreenButtonStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
   <Setter Property="Background" Value="Green" />
   <Setter Property="Foreground" Value="White" />
</Style>

因此,隐式样式将响应更改系统外观:

<Style x:Key="ImplicitGreenButtonStyle" TargetType="Button">
    <Setter Property="Background" Value="Green" />
    <Setter Property="Foreground" Value="White" />
</Style>

此外,在示例中包含 ThemeHelper 中的有用代码,其中一些主题功能。

*编辑#2 *

如果我理解正确,首先需要获取系统主题名称。这个动作可以通过几种方式完成。

第一种是使用库“UxTheme.dll”中的Win32函数,如 GetCurrentThemeName()

[DllImport("uxtheme.dll", CharSet = CharSet.Auto)]
public static extern int GetCurrentThemeName(StringBuilder pszThemeFileName, int dwMaxNameChars, StringBuilder pszColorBuff, int dwMaxColorChars, StringBuilder pszSizeBuff, int cchMaxSizeChars);

StringBuilder stringThemeName = new StringBuilder(260);
StringBuilder stringColorName = new StringBuilder(260);
StringBuilder stringSizeName = new StringBuilder(260);

Int32 s = GetCurrentThemeName(stringThemeName, 260, stringColorName, 260, stringSizeName, 260);

但我有这个功能没有正确收到一些系统主题名称,比如“Classic”。所以我尝试了另一种方式。

第二种方式是从注册表获取主题名称。在路径“HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Themes \ LastTheme”包含当前系统主题。那么我们是否需要通过窗口挂钩拦截“更改系统主题事件”。

以下是几个按钮的示例,样式因系统主题而异:

添加到窗口:

SourceInitialized="Window_SourceInitialized"

样式:

<Style x:Key="DefaultStyle" BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="CadetBlue" />
</Style>

<Style x:Key="LunaStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Blue" />
    <Setter Property="Foreground" Value="White" />
</Style>

<Style x:Key="ClassicStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Gray" />
    <Setter Property="Foreground" Value="Black" />
</Style>

主网格:

<Grid>
    <Button Style="{StaticResource DefaultStyle}" Content="Default button" Width="100" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" />
    <Button Name="ChangeButtonStyle" Content="Changes style" Width="100" Height="30" VerticalAlignment="Top" HorizontalAlignment="Right" />
    <TextBlock Name="CurrentTheme" FontSize="16" Text="Null" Width="150" Height="30" HorizontalAlignment="Center" VerticalAlignment="Top" />
</Grid>

在代码中:

拦截“更改系统主题事件”:

private IntPtr hwnd;
private HwndSource hsource;

private void Window_SourceInitialized(object sender, EventArgs e)
{
    if ((hwnd = new WindowInteropHelper(this).Handle) == IntPtr.Zero)
    {
        throw new InvalidOperationException("Could not get window handle.");
    }

    hsource = HwndSource.FromHwnd(hwnd);
    hsource.AddHook(WndProc);
}

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    switch (msg)
    {
        case 0x31A:          // Define this as WM_DWMCOMPOSITIONCHANGED for Windows 7
        case 0x31E:          // Define this as WM_THEMECHANGED

        // Action on the change system theme
        GetThemeName(SubKey, Value); 

        return IntPtr.Zero;

        default:

        return IntPtr.Zero;
     }
}

获取系统主题名称并设置样式:

public string SubKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\LastTheme";
public string Value = "ThemeFile";

private void GetThemeName(string OpenKey, string Value)
{
    RegistryKey pRegKey = Registry.CurrentUser;
    pRegKey = pRegKey.OpenSubKey(OpenKey);
    Object val = pRegKey.GetValue(Value);
    string NameThemeFile = val as string;

    if (NameThemeFile.IndexOf("Luna") != -1)
    {
        ChangeButtonStyle.Style = this.FindResource("LunaStyle") as Style;
        CurrentTheme.Text = "Luna";
    }

    if (NameThemeFile.IndexOf("Classic") != -1)
    {
        ChangeButtonStyle.Style = this.FindResource("ClassicStyle") as Style;
        CurrentTheme.Text = "Classic";
    }
}

这种方法不是最好的,但会开始。

相关问题