Skip to content

Instantly share code, notes, and snippets.

@ido-ran
Created October 13, 2010 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ido-ran/625081 to your computer and use it in GitHub Desktop.
Save ido-ran/625081 to your computer and use it in GitHub Desktop.
<UserControl x:Class="MultiRangeSlider.MultiRangeSliderControl"
x:Name="root"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinWidth="50">
<UserControl.Resources>
<ControlTemplate x:Key="StripedSlider" TargetType="{x:Type Slider}">
<Border SnapsToDevicePixels="true"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle x:Name="PART_SelectionRange"/>
<Track x:Name="PART_Track" Grid.Row="1">
<Track.Thumb>
<Thumb x:Name="Thumb" Width="10" Height="18" Background="{TemplateBinding Background}">
<!--<Thumb.Template>
<ControlTemplate TargetType="Thumb">
<Rectangle Fill="Red"
Stroke="Black"
StrokeThickness="1"
Width="10"
Height="18"
SnapsToDevicePixels="True"/>
</ControlTemplate>
</Thumb.Template>-->
</Thumb>
</Track.Thumb>
</Track>
</Grid>
</Border>
</ControlTemplate>
</UserControl.Resources>
<Grid>
<Slider x:Name="Value1Slider"
Minimum="{Binding ElementName=root, Path=Minimum}"
Maximum="{Binding ElementName=root, Path=Maximum}"
Value="{Binding ElementName=root, Path=Value1, Mode=TwoWay}"
Background="Green"
Template="{StaticResource StripedSlider}" />
<Slider x:Name="Value2Slider"
Minimum="{Binding ElementName=root, Path=Minimum}"
Maximum="{Binding ElementName=root, Path=Maximum}"
Value="{Binding ElementName=root, Path=Value2, Mode=TwoWay}"
Background="Yellow"
Template="{StaticResource StripedSlider}" />
<Slider x:Name="Value3Slider"
Minimum="{Binding ElementName=root, Path=Minimum}"
Maximum="{Binding ElementName=root, Path=Maximum}"
Value="{Binding ElementName=root, Path=Value3, Mode=TwoWay}"
Background="Blue"
Template="{StaticResource StripedSlider}" />
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MultiRangeSlider {
/// <summary>
/// Interaction logic for MultiRangeSliderControl.xaml
/// </summary>
public partial class MultiRangeSliderControl : UserControl {
public MultiRangeSliderControl() {
InitializeComponent();
}
public double Minimum {
get { return (double)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
public static readonly DependencyProperty MinimumProperty =
DependencyProperty.Register("Minimum", typeof(double), typeof(MultiRangeSliderControl), new UIPropertyMetadata(0d));
public double Value1 {
get { return (double)GetValue(Value1Property); }
set { SetValue(Value1Property, value); }
}
public static readonly DependencyProperty Value1Property =
DependencyProperty.Register("Value1", typeof(double), typeof(MultiRangeSliderControl), new FrameworkPropertyMetadata(0d, Value1Changed, CoerceValue1));
public double Value2 {
get { return (double)GetValue(Value2Property); }
set { SetValue(Value2Property, value); }
}
public static readonly DependencyProperty Value2Property =
DependencyProperty.Register("Value2", typeof(double), typeof(MultiRangeSliderControl), new FrameworkPropertyMetadata(0d, Value2Changed, CoerceValue2));
public double Value3 {
get { return (double)GetValue(Value3Property); }
set { SetValue(Value3Property, value); }
}
public static readonly DependencyProperty Value3Property =
DependencyProperty.Register("Value3", typeof(double), typeof(MultiRangeSliderControl), new FrameworkPropertyMetadata(0d, Value3Changed, CoerceValue3));
public double Maximum {
get { return (double)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
public static readonly DependencyProperty MaximumProperty =
DependencyProperty.Register("Maximum", typeof(double), typeof(MultiRangeSliderControl), new UIPropertyMetadata(1d));
private static void Value1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var self = (MultiRangeSliderControl)d;
self.CoerceValue(Value2Property);
}
private static void Value2Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var self = (MultiRangeSliderControl)d;
self.CoerceValue(Value1Property);
self.CoerceValue(Value3Property);
}
private static void Value3Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var self = (MultiRangeSliderControl)d;
self.CoerceValue(Value2Property);
}
private static object CoerceValue1(DependencyObject d, object baseValue) {
var self = (MultiRangeSliderControl)d;
var v = (double)baseValue;
if (v > self.Value2) return self.Value2;
else return baseValue;
}
private static object CoerceValue2(DependencyObject d, object baseValue) {
var self = (MultiRangeSliderControl)d;
var v = (double)baseValue;
if (v > self.Value3) return self.Value3;
else if (v < self.Value1) return self.Value1;
else return baseValue;
}
private static object CoerceValue3(DependencyObject d, object baseValue) {
var self = (MultiRangeSliderControl)d;
var v = (double)baseValue;
if (v < self.Value2) return self.Value2;
else return baseValue;
}
}
}
<Window x:Class="MultiRangeSlider.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MultiRangeSlider"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<local:MultiRangeSliderControl Grid.ColumnSpan="3" x:Name="t" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding ElementName=t, Path=Value1}" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding ElementName=t, Path=Value2}" />
<TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding ElementName=t, Path=Value3}" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MultiRangeSlider {
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment