wpf 更改滑动条颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19123301/
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
Change slider bar color
提问by Sturm
It should be very easy to do this but I haven't found the information that I need. What I want is as simple as changing the color of the slider bar:
这样做应该很容易,但我还没有找到我需要的信息。我想要的就像更改滑块条的颜色一样简单:


I'm using ModernUI and the default bar color is very similar to my background and I want to make it a bit lighter.
我使用的是 ModernUI,默认的条形颜色与我的背景非常相似,我想让它更亮一点。
回答by Tico
You should be able to change it editing the template.
您应该能够通过编辑模板来更改它。
Right click your Slider, Edit Template -> Edit Copy.;.
右键单击您的滑块,编辑模板 -> 编辑副本。;。
A new window will appear asking you where VS should put the XAML code for the ControlTemplate and Styles. Chek the tags and such.
将出现一个新窗口,询问您 VS 应将 ControlTemplate 和 Styles 的 XAML 代码放在哪里。检查标签等。
Good luck!
祝你好运!
Edit:
Ok, here it goes.
编辑:
好的,就这样。
Assuming that you already have a ModernUI App, create a new folder called Assets, right click it Add -> New Item... -> ModernUI Theme. Call it whatever you like it.
假设您已经有一个 ModernUI App,创建一个名为 的新文件夹Assets,右键单击它Add -> New Item... -> ModernUI Theme。喜欢什么就叫什么。
Inside the newly created XAML file insert these SolidColorBrushunder the AccentColorColortag:
在新创建的 XAML 文件中,将这些插入SolidColorBrush到AccentColorColor标记下:
<SolidColorBrush x:Key="SliderSelectionBackground" Color="Red" />
<SolidColorBrush x:Key="SliderSelectionBorder" Color="Red" />
<SolidColorBrush x:Key="SliderThumbBackground" Color="Red" />
<SolidColorBrush x:Key="SliderThumbBackgroundDisabled" Color="Red" />
<SolidColorBrush x:Key="SliderThumbBackgroundDragging" Color="Red" />
<SolidColorBrush x:Key="SliderThumbBackgroundHover" Color="Red" />
<SolidColorBrush x:Key="SliderThumbBorder" Color="Red" />
<SolidColorBrush x:Key="SliderThumbBorderDisabled" Color="Red" />
<SolidColorBrush x:Key="SliderThumbBorderDragging" Color="Red" />
<SolidColorBrush x:Key="SliderThumbBorderHover" Color="Red" />
Each one of these represents a state of the Thumb(the slider "rectangle"). After that open your App.xamlfile and include your theme there (this is what my file looks like):
这些中的每一个都代表Thumb(滑块“矩形”)的状态。之后打开您的App.xaml文件并在其中包含您的主题(这就是我的文件的样子):
<Application x:Class="ModernUIApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Light.xaml"/>
<ResourceDictionary Source="/Assets/ModernUI.Theme1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
The <ResourceDictionary Source="/Assets/ModernUI.Theme1.xaml" />bit represents my theme.
Setting all the colors to Red, this is what it looked like:
该<ResourceDictionary Source="/Assets/ModernUI.Theme1.xaml" />位代表我的主题。将所有颜色设置为红色,这是它的样子:


I guess that's more clear! Hope you like it.
我想这更清楚!希望你喜欢。
EDIT:
It will change when you apply your theme. But, as you're familiar with styles, I'm sending the complete template. What you can do is create a UserDictionary with only this template and when you you want to use it, change the slider Template property. You'll want to change only the Thumb Tags. Pastebin code
编辑:
当你应用你的主题时它会改变。但是,由于您熟悉样式,因此我将发送完整的模板。您可以做的是仅使用此模板创建一个 UserDictionary,当您想使用它时,更改滑块模板属性。您只需要更改 Thumb 标签。粘贴代码
And if you want to change only THIS one put the template between <Windows.Resources>or <Slider.Resources>- Another option would be create your own control
如果您只想更改此模板,请将模板放在<Windows.Resources>或之间<Slider.Resources>- 另一种选择是创建您自己的控件
回答by Alezis
I found two approaches:
我找到了两种方法:
You can customize your slider by insert corresponding brushes in appropriate
Slider.Resourcessection.You can add brushes to separate xaml file with dictionary and then merge it with corresponding slider in the
Slider.Resources. In some cases it fits better because you can change colors of few controls at once.
您可以通过在适当的
Slider.Resources部分插入相应的画笔来自定义滑块。您可以将画笔添加到带有字典的单独的 xaml 文件中,然后将其与
Slider.Resources. 在某些情况下,它更适合,因为您可以一次更改几个控件的颜色。
Any does not need to changing of the control's template.
Any 不需要更改控件的模板。
Both approaches are presented below:
下面介绍了这两种方法:
Page1.xaml
页面1.xaml
<Grid Style="{StaticResource ContentRoot}">
<StackPanel>
<!-- Slider with default theme and colors from ModernUI -->
<Slider/>
<!-- Slider with custom colors from approach 1 -->
<Slider>
<Slider.Resources>
<SolidColorBrush x:Key="SliderSelectionBackground" Color="Green" />
<SolidColorBrush x:Key="SliderSelectionBorder" Color="Green" />
<SolidColorBrush x:Key="SliderThumbBackground" Color="Green" />
<SolidColorBrush x:Key="SliderThumbBackgroundDisabled" Color="Green" />
<SolidColorBrush x:Key="SliderThumbBackgroundDragging" Color="Green" />
<SolidColorBrush x:Key="SliderThumbBackgroundHover" Color="Green" />
<SolidColorBrush x:Key="SliderThumbBorder" Color="Green" />
<SolidColorBrush x:Key="SliderThumbBorderDisabled" Color="Green" />
<SolidColorBrush x:Key="SliderThumbBorderDragging" Color="Green" />
<SolidColorBrush x:Key="SliderThumbBorderHover" Color="Green" />
</Slider.Resources>
</Slider>
<!-- Slider with custom colors from approach 2 -->
<Slider>
<Slider.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Slider.Resources>
</Slider>
</StackPanel>
</Grid>
Dictionary1.xaml
字典1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="SliderSelectionBackground" Color="Blue" />
<SolidColorBrush x:Key="SliderSelectionBorder" Color="Blue" />
<SolidColorBrush x:Key="SliderThumbBackground" Color="Blue" />
<SolidColorBrush x:Key="SliderThumbBackgroundDisabled" Color="Blue" />
<SolidColorBrush x:Key="SliderThumbBackgroundDragging" Color="Blue" />
<SolidColorBrush x:Key="SliderThumbBackgroundHover" Color="Blue" />
<SolidColorBrush x:Key="SliderThumbBorder" Color="Blue" />
<SolidColorBrush x:Key="SliderThumbBorderDisabled" Color="Blue" />
<SolidColorBrush x:Key="SliderThumbBorderDragging" Color="Blue" />
<SolidColorBrush x:Key="SliderThumbBorderHover" Color="Blue" />
</ResourceDictionary>
As result you get following:
结果你得到以下:


回答by Santux
Here you have the templates you should use: Slider Styles and Templates
这里有您应该使用的模板:滑块样式和模板
The property you are looking to edit is the TrackBackground.BackGround.
您要编辑的属性是 TrackBackground.BackGround。
If you define a style for this control template and put it either in you app.xaml or in the window.resources or in any other file, as long as you give it a key you can use it in a specific slider through the "Style" property of that slider using that same key.
如果您为此控件模板定义了样式并将其放入您的 app.xaml 或 window.resources 或任何其他文件中,只要您给它一个键,您就可以通过“样式”在特定滑块中使用它" 使用相同键的滑块的属性。
回答by A.J.Bauer
Windows 8.1 Store/Phone Apps.
Windows 8.1 商店/电话应用程序。
Add this to the App.xaml and change the color values to your liking:
将此添加到 App.xaml 并根据您的喜好更改颜色值:
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<SolidColorBrush x:Key="SliderTrackDecreaseBackgroundThemeBrush" Color="#FFFF0000" />
<SolidColorBrush x:Key="SliderTrackDecreasePointerOverBackgroundThemeBrush" Color="#FF00FF00" />
<SolidColorBrush x:Key="SliderTrackDecreasePressedBackgroundThemeBrush" Color="#FF0000FF" />
</ResourceDictionary>
<ResourceDictionary x:Key="Light">
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrastBlack">
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrastWhite">
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
You probably only want to change the slider for the default theme and probably only the three color values shown above. For all colors / resources that you can change, see this link at MSDN: Slider styles and templates.
您可能只想更改默认主题的滑块,并且可能只想更改上面显示的三个颜色值。有关您可以更改的所有颜色/资源,请参阅 MSDN 上的此链接:滑块样式和模板。
回答by andymule
For what it's worth, the only way I could change the Slider Thumb color on Win10 UWP for Phone was to overwrite the System Foreground brush. (You can also apparently completely re-template the whole Slider)
值得一提的是,我可以在 Win10 UWP for Phone 上更改滑块拇指颜色的唯一方法是覆盖系统前景画笔。(您显然也可以完全重新模板化整个 Slider)
So, I put into my App.xaml
所以,我把我的 App.xaml
<Application
x:Class="App1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
RequestedTheme="Dark">
<Application.Resources>
<SolidColorBrush x:Key="SystemControlForegroundAccentBrush" Color="White" />
</Application.Resources>
</Application>
The addition to Application.Resources is the really important thing here. It's where we're overwriting the Foreground color for ALL common elements, like Checkbox, ContentDialog, ProgressRing, etc.... So, that's the downside to this method too.
对 Application.Resources 的添加在这里非常重要。这是我们覆盖所有常见元素的前景色的地方,比如 Checkbox、ContentDialog、ProgressRing 等......所以,这也是这种方法的缺点。
Changing the Thumb color on a Slider is a known problem point for XAML UWP. Microsoft has plans to make it easier in the immediate future.
更改滑块上的拇指颜色是 XAML UWP 的一个已知问题点。微软计划在不久的将来让它变得更容易。
回答by Javert
Foreground property is used to fill the "completed" part of the slider with a particular color. (Background does the uncompleted part.)
Foreground 属性用于用特定颜色填充滑块的“完成”部分。(背景是未完成的部分。)
<Slider Value="40" Maximum="100" Foreground="Red" />

