C# 在WPF中,滑块轨迹栏控制是否可以有多个背景?
我正在尝试创建一个多色滑块。我有三种颜色:红、绿、蓝。 我正在尝试设置滑块轨迹栏的颜色为0-33红色、34-66绿色、67-100蓝色 我不知道该怎么做 我可以设置滑块的背景,但我得到了一个渐变,我想要类似的东西,但不是渐变,只是纯色C# 在WPF中,滑块轨迹栏控制是否可以有多个背景?,c#,wpf,slider,C#,Wpf,Slider,我正在尝试创建一个多色滑块。我有三种颜色:红、绿、蓝。 我正在尝试设置滑块轨迹栏的颜色为0-33红色、34-66绿色、67-100蓝色 我不知道该怎么做 我可以设置滑块的背景,但我得到了一个渐变,我想要类似的东西,但不是渐变,只是纯色 var brush = new LinearGradientBrush(new GradientStopCollection() { new GradientStop(Colors.Red, 0.0),
var brush = new LinearGradientBrush(new GradientStopCollection() {
new GradientStop(Colors.Red, 0.0),
new GradientStop(Colors.Blue, 0.3),
new GradientStop(Colors.Green, 0.6),
});
TestBar.Background = brush;
我正在尝试做一些动态的事情
如果我有这样的课
class SomeObject {
public int From;
public int To;
public string Color;
}
我可以毫无问题地更改轨迹栏的颜色。我在这里尝试了我认为您需要的操作,使用了一个完整的自定义控件。可能有更简单的方法可以做到这一点,但是因为你想把控件的背景分成三个部分,并涂上颜色,所以我认为合乎逻辑的做法是将三个矩形放入控件模板中并涂上颜色 该代码包含一个ColorSlider控件,三种颜色的依赖属性分别为Color1、Color2、Color3(画笔类型),三种宽度的依赖属性分别为Width1、Width2、Width3(双精度) 为此,我们设置了一个ColorSlider类,用于注册依赖项属性(摘录): 我们需要一份以ColorSlider为目标的滑块控制模板的副本,然后在其网格中使用Stackpanel,如下所示(清晰摘录):
希望能有所帮助,这显然有点基本。可能与事实上没有重复。我只是举了一个Youtube颜色方案的例子。我的轨迹栏应该在不同的范围内有不同的颜色:1-10黄色、11-82红色、83-100绿色等等。“如果我有这样的课程……我可以毫无问题地更改轨迹栏的颜色。”。那你很好。我不知道你会如何使用这样的类来做,但听起来你是这样做的,那么你的问题是什么?“动态的东西”是什么意思?有什么特别有活力的东西,或者只是任何有活力的旧东西?“动态”对你来说意味着什么?也许你可以在MS Paint中画出你想要的东西的粗略图片,并将其添加到你的问题中。如果你看一下模板:请注意,拇指放在上面的一个轨迹实际上是两个按钮。然后使其透明,并将轨迹的背景设置为渐变。这是一个很好的例子,但它是否可以是动态的,就像我希望根据用户输入有6个宽度一样?最简单的方法是创建6个矩形,具有6个宽度和颜色,而不是上面所述的3个(或用户可以指定的最大宽度),如果你想显示更少的矩形,就把宽度设为零。好的,谢谢,我想我知道如何实现它。用你的例子,稍加调整,我就能做到。
public class ColorSlider : Slider
{
public static readonly DependencyProperty Width1Property = DependencyProperty.Register("Width1", typeof(double), typeof(ColorSlider), new UIPropertyMetadata(0.0));
[TypeConverter(typeof(LengthConverter))]
public double Width1
{
get { return (double)GetValue(Width1Property); }
set { SetValue(Width1Property, value); }
}
<StackPanel Grid.Row="1" Panel.ZIndex="-1" Orientation="Horizontal">
<Rectangle Fill="{TemplateBinding Color1}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width1}" />
<Rectangle Fill="{TemplateBinding Color2}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width2}" />
<Rectangle Fill="{TemplateBinding Color3}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width3}" />
</StackPanel>
<local:ColorSlider x:Name="MyColorSlider" Template="{DynamicResource ColorSliderControlTemplate}" Width="200" Height="50"
Color1="Red" Color2="Green" Color3="Blue" Width1="68" Width2="66" Width3="66"></local:ColorSlider>
MyColorSlider.Width3 = 20;
MyColorSlider.Color1 = new SolidColorBrush(Colors.Orange);