组合框背景未在 Windows 8 中应用 [英] Combobox background not being applied in windows 8
问题描述
我对 wpf 主题有点困惑.我想让 wpf 屏幕在 Vista、Windows 7 和 Windows 8 上看起来相同.所以我已经相应地设置了组件的样式,除了在 Windows 8 上运行时,它们不会造成问题.例如,我有一个组合框,我是像这样在 xaml 中更改其默认背景.
组合框背景属性在 Windows 8 中无效,我得到的只是一个带有右侧箭头的扁平矩形(默认的 Windows 8 组合框,设计相当糟糕!).
所以,我的问题是如何让组合框在所有版本的 Windows 上看起来都一样.我尝试在我的 App.xaml 中添加 windows Aero 主题,如下所示,但它对组合框显示没有影响.这是我添加 Aero 主题的方式
<资源字典><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="/PresentationFramework.Aero;component/themes/aero.normalcolor.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary>
关于主题还有另一个疑问.我正在 Windows 7 机器上构建 wpf 应用程序,默认情况下(我相信)设置了 Aero Theme.因此,在 Windows 7 机器上查看时,我所有的样式都基于 Aero 主题.如果我在 XP 上运行应用程序会发生什么.那么我是否需要在 App.xaml 中为资源字典(Aero 主题)添加一个条目,如上面代码中所列?
我知道我的问题有点含糊,但相信我,我真的很困惑不同 Windows 版本上 wpf 的默认主题.
我仍然无法根据我的需要设置组合框的样式.组合框仍然看起来像一个灰色矩形.
这就是我所做的.我从微软的网站下载了 Aero.NormalColor.xaml 并包含在应用程序的主题文件夹中.然后我在 App.xaml 中添加了以下内容
<ResourceDictionary.MergedDictionaries><ResourceDictionary Source="/Themes/Aero.NormalColor.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary>
然后我编译了应用程序并部署在 Windows 8 上.仍然是之前显示的相同组合框.请注意,所有其他元素都会根据主题正确设置样式.我对 Luna.Metallic.xaml 做了同样的事情,除了 ComboBox 之外,每个元素都被设置了样式.
我相信当我加载一个特定的主题时,它定义了 ControlTemplate 的样式,那么它应该由 wpf 选择.我很困惑为什么只有 ComboBox 即使给它一个 Aero(或 Luna)控制模板也不会改变它的外观.有什么想法吗?
EDIT-2Windows 8 上组合框外观的屏幕截图
好吧,ComboBox
可点击区域实际上是一个 ToggleButton
如果您查看 Windows-8 中该 ToggleButton
的 Style
,您会看到如下内容:
<Border x:Name="templateRoot"背景="{StaticResource ComboBox.Static.Background}"BorderBrush="{StaticResource ComboBox.Static.Border}"BorderThickness="{TemplateBinding BorderThickness}"SnapsToDevicePixels="true">...
从上面可以看出,Background
使用的不是 {TemplateBinding Background}
而是 {StaticResource ComboBox.Static.Background}
.因此,当您在 Windows-8 中为该 ComboBox
Background
属性时,为什么看不到任何效果如果您希望在不同的操作系统版本中使用 Style
(无需回溯并不断检查新版本是否搞砸了您的覆盖),简单的规则是创建强>你自己.
创建一个 Style
并将其设置为由 TargetType
应用而没有 Key
以自动应用.这样,在任何操作系统中,使用的是您的 Style
而不是默认的底层样式.
因此,这可以保证您的代码在每个操作系统上都按预期运行.以任何操作系统的默认风格为基础,并根据您的喜好进行调整.
旁注:
从可用性 POV 来看,在 Windows-8 上运行的应用程序中为用户提供 Windows-7 ComboBox
不是很好(除非你的整个应用程序看起来像一个 Windows-7 应用程序,甚至更差).您期望用户习惯您的应用程序的样式并忘记他在他的操作系统中使用的使用基于操作系统的默认样式的每个其他应用程序中习惯的内容.如果您有具体的理由这样做,请继续,但要考虑其影响.
举个例子,你说 Windows-8 风格是你不喜欢的东西,我正好相反.实际上,我确实喜欢 Windows-8 干净简单的外观.不会通过闪烁的渐变和让您远离摆在他们面前的内容的事物分散用户体验的注意力.这是一个永远持续下去的争论.请注意最终用户的期望和想法,而不仅仅是您在编写程序时认为好的.
更新:
请先评论相关答案.你的回答和你的评论更新没有关系.
好的,至于您的问题编辑,您尝试的方法在 windows-8 中不起作用,因为 PresentationFramework.Aero.dll
在保存 Aero 的 Windows-8 中不存在.NormalColor.xaml
.在 Windows-8 中,您的选项是 PresentationFramework.Aero2.dll
这是默认值和 PresentationFramework.AeroLite.dll
我认为 Windows Server 2012(不确定)使用了>
尝试在 Windows-8 上编译您的程序,您会发现它甚至不想编译.
您必须明确地向您的项目添加对 PresentationFramework.Aero
和 PresentationUI
(我认为它是 .net3 的一部分)的引用.
然后你必须将你的 Aero.NormalColor.xaml
编辑成这样:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"xmlns:ui="clr-namespace:System.Windows.Documents;assembly=PresentationUI">...
^^ 我们明确说明了 Aero Theme 的程序集.我不使用 Windows-7,所以我不确定这是否就足够了.但你可以试一试.
尝试在 Windows-8 中编译您的代码以确保它可以在 Windows-8 上正常运行
I am a little confused with wpf themes. I would like to have the wpf screens look the same on Vista, Windows 7 and Windows 8. So I have styled the components accordingly and they don't pose problems except when run on Windows 8. For example I have a combobox and I am changing its default background in xaml like this.
<Style TargetType="{x:Type ComboBox}" >
<Setter Property="FontStyle" Value="Normal"/>
<Setter Property="Height" Value="24" />
<Setter Property="Background" Value="{StaticResource GradientButtonBackgroundBrush}"/>
</Style>
The combobox Background property has no effect in windows 8 and all I get is a flat rectangle with a arrow on right (the default windows 8 combobox, which is rather poorly designed!).
So, my question is that how do I get the combobox look the same on all version of windows. I tried adding windows Aero theme in my App.xaml like below, but it has no effect on the combobox display. Here is how I added Aero theme
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/aero.normalcolor.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
There is also another doubt regarding themes. I am building the wpf application on a windows 7 machine, which by default (I believe) has Aero Theme set. So, all my styles are based on the Aero theme when viewed on Windows 7 machine. What happens if I run the application on say XP. Then do I need to add an entry for the resource dictionary (Aero theme) in App.xaml as listed in code above?
I know my question is a bit vague, but believe me, I am really confused with default themes of wpf on different Windows versions.
EDIT: I still can't get combobox to style according to my needs. The combobox still appears like a gray rectangle.
Here is what I did. I downloaded the Aero.NormalColor.xaml from microsoft's site and included in themes folder of application. Then I added the following in App.xaml
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/Aero.NormalColor.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Then I Compiled the application and deployed on Windows 8. Still the same combobox as was shown previously. Note that all other elements get styled properly according to the theme. I did the same with Luna.Metallic.xaml and every element gets styled except the ComboBox.
I believe that when I load a particular theme, which defines styles with ControlTemplate, then it should be picked by wpf. I am confused as to why only the ComboBox even after giving it a Aero (or Luna) Control Template doesn't change its appearance. Any ideas ?
EDIT-2 Screen shot of combobox appearance on Windows 8
Well the ComboBox
clickable area is actually a ToggleButton
and if you look at the Style
for that ToggleButton
in Windows-8, you see something like:
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="templateRoot"
Background="{StaticResource ComboBox.Static.Background}"
BorderBrush="{StaticResource ComboBox.Static.Border}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="true">
...
As you can see from above, Background
used is not a {TemplateBinding Background}
but {StaticResource ComboBox.Static.Background}
. Hence why you see no effect when you set the Background
property in Windows-8 for that ComboBox
If your looking to carry a Style
across the different OS Versions(Without having to backtrack and keep checking if new versions screwed up your over-rides), simple rule is Create it yourself.
Create a Style
and set it to be applied by TargetType
and without a Key
to get applied automatically. That way in any OS it's your Style
that gets used and not the default underlying one.
This thus guarantees your code run's as you expect on every single OS. Base your Style on the default's of any OS and tweak it to your heart's content.
side-note:
From a usability POV giving the user a Windows-7 ComboBox
in an app that run's on Windows-8 is not very nice(unless your entire app looks like a Windows-7 app which is even worse). Your expecting the user to get used to your app's Style's and forget what he's used to from every other app he uses in his OS that use default Styles based on OS. If you have specific reasons for doing so, go ahead but do consider the implications.
Just for example you stated the Windows-8 Style is something your not a fan of, well I'm the opposite. I actually do like the Windows-8 clean and simple look. No distractions to the UserExperience with flashing gradients and things that throw you off the content your putting in front of them. This is an argument that goes on forever. Just be warned abt what the end-user expects and thinks than just what you think is good while writing your program.
Update:
Firstly comment on the relevant answer please. Your answer and your comment update has no relation.
Ok and as for your question edit, what you tried has not worked in windows-8 because PresentationFramework.Aero.dll
does not exist in Windows-8 which is what holds Aero.NormalColor.xaml
. In Windows-8 your options are PresentationFramework.Aero2.dll
which is default and PresentationFramework.AeroLite.dll
which I think is used by Windows Server 2012(Not Sure)
Try to compile your program on Windows-8 and you'll see it does not even want to compile.
You'll have to explicitly add a reference to PresentationFramework.Aero
and also PresentationUI
(which i think is part of .net3) to your project.
Then you'll have to edit your Aero.NormalColor.xaml
to something like:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:ui="clr-namespace:System.Windows.Documents;assembly=PresentationUI">
...
^^ we explicitly state the assembly for Aero Theme. I don't use Windows-7 so Am not sure if that's all that's needed. but you can give that a try.
Try to compile your code in Windows-8 to make sure it will work fine on Windows-8
这篇关于组合框背景未在 Windows 8 中应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!