如何在 WPF 应用程序中禁用 XP 主题? [英] How to disable XP themes in WPF application?

查看:27
本文介绍了如何在 WPF 应用程序中禁用 XP 主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 应用程序(.NET 3.0、VS2008),它在选项卡控件中显示数据.此应用程序中几乎所有控件都需要自定义颜色:基本上是白色前景和绿色背景.

I have a WPF application (.NET 3.0, VS2008) that displays data in a tab control. Custom colors are required on virtually all controls in this application : basically white foreground and green background.

我的问题是,当 XP 主题(比如 Luna)处于活动状态时,它会自动应用于渲染控件,以便我的白色文本写在……白色背景上.例如在活动选项卡项目标题上:

My problem is that when an XP theme (say Luna) is active, it is automatically applied to render controls so that my white text is written on ... a white background. For instance on the active tab item header :

我试过了:

  • 从生成的应用程序中删除嵌入的清单文件(在项目属性中):没用.
  • 强制在应用程序资源中使用 Windows 经典主题:

  • to remove the embedded manifest file from the generated application (in the project properties) : useless.
  • to force the use of the Windows Classic theme in the application resources :

<ResourceDictionary.MergedDictionaries>
  <ResourceDictionary Source="/PresentationFramework.Classic;V3.0.0.0;31bf3856ad364e35;component/themes/classic.xaml" />
</ResourceDictionary.MergedDictionaries>

最后一次尝试对所有控件都很有效,除了仍然如上显示的选项卡控件.

This last attempt worked great for all controls, except the tab control which still displays as above.

有什么想法吗?

更新:我怀疑这种行为是由于我必须应用于选项卡控件的自定义样式:

Update : I am suspecting this behaviour is due to the custom style I have to apply to the tab control :

<Window.Resources>
  <Style x:Key="Custom">
    <Setter Property="Control.Background" Value="#FF47C7C8" />
    <Setter Property="Control.Foreground" Value="White" />
  </Style>
  <Style TargetType="TabControl" BasedOn="{StaticResource Custom}" />
</Window.Resources>

那么如何获得自定义颜色的经典主题?

So how can I get the classic theme with custom colors ?

推荐答案

为了确保跨操作系统的行为和外观一致,最好的办法是重新模板化 TabItem 控件,然后使用触发器修改部件选择 TabItem 时的新模板.尝试如下操作:

Your best bet, to ensure a consistent behavior and appearance across operating systems, would be to re-template the TabItem control and then use a Trigger to modify a part of your new template when a TabItem is selected. Try something like the following:

<Grid>
<Grid.Resources>
  <Style x:Key="Custom">
    <Setter Property="Control.Background" Value="#FF47C7C8"/>
    <Setter Property="Control.BorderBrush" Value="#FF47C7C8"/>
    <Setter Property="Control.Foreground" Value="White"/>
  </Style>
  <Style BasedOn="{StaticResource Custom}" TargetType="TabControl"/>
  <Style TargetType="TabItem">
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type TabItem}">
          <Grid>
            <Border
              Name="Border"
              Background="#FF47C7C8"
              BorderBrush="#FFFFFF"
              BorderThickness="1,1,1,1"
              CornerRadius="2,2,0,0">
              <ContentPresenter
                x:Name="ContentSite"
                HorizontalAlignment="Center"
                Margin="12,2,12,2"
                VerticalAlignment="Center"
                ContentSource="Header"
                RecognizesAccessKey="True"/>
            </Border>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="IsSelected" Value="True">
              <Setter Property="Panel.ZIndex" Value="100"/>
              <Setter TargetName="Border" Property="Background" Value="#FF47C7C8"/>
              <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0"/>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</Grid.Resources>
<TabControl>
  <TabItem Header="Item 1"/>
  <TabItem Header="Item 2"/>
  <TabItem Header="Item 3"/>
  <TabItem Header="Item 4"/>
</TabControl>

好机会!

这篇关于如何在 WPF 应用程序中禁用 XP 主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆