using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace XHWK.WKTool
{
///
/// UC_MultiRangeSlider.xaml 的交互逻辑
///
public partial class UC_MultiRangeSlider : UserControl
{
public UC_MultiRangeSlider()
{
InitializeComponent();
}
#region 私有变量
private static int _width = 150; // 拖动条初始宽度
private static int _height = 30; // 高度
private static double _min = 0; // 最小值
private static double _max = 1000; // 最大值
private static int _freq = 1; // 出现刻度的间距
#endregion
///
/// 裁剪矩阵(头)
///
private Rect StartRect
{
get
{
return (Rect)GetValue(StartRectProperty);
}
set
{
SetValue(StartRectProperty, value);
}
}
private static readonly DependencyProperty StartRectProperty =
DependencyProperty.Register("StartRect", typeof(Rect), typeof(UC_MultiRangeSlider));
///
/// 裁剪矩阵(尾)
///
private Rect EndRect
{
get { return (Rect)GetValue(EndRectProperty); }
set { SetValue(EndRectProperty, value); }
}
private static readonly DependencyProperty EndRectProperty =
DependencyProperty.Register("EndRect", typeof(Rect), typeof(UC_MultiRangeSlider));
#region 公开依赖属性
///
/// 刻度间距,默认为10
///
public int SliderTickFrequency
{
get { return (int)GetValue(SliderTickFrequencyProperty); }
set { SetValue(SliderTickFrequencyProperty, value); }
}
public static readonly DependencyProperty SliderTickFrequencyProperty =
DependencyProperty.Register("SliderTickFrequency", typeof(int), typeof(UC_MultiRangeSlider), new PropertyMetadata(_freq));
///
/// 控件高度,默认为30
///
public int SilderHeight
{
get { return (int)GetValue(SilderHeightProperty); }
set { SetValue(SilderHeightProperty, value); }
}
public static readonly DependencyProperty SilderHeightProperty =
DependencyProperty.Register("SilderHeight", typeof(int), typeof(UC_MultiRangeSlider), new PropertyMetadata(_height));
///
/// 拖动条宽度,默认为150
///
public int SilderWidth
{
get { return (int)GetValue(SilderWidthProperty); }
set { SetValue(SilderWidthProperty, value); }
}
public static readonly DependencyProperty SilderWidthProperty =
DependencyProperty.Register("SilderWidth", typeof(int), typeof(UC_MultiRangeSlider), new PropertyMetadata(_width));
///
/// 最小值,默认为0
///
public double Minimum
{
get { return (double)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
public static readonly DependencyProperty MinimumProperty =
DependencyProperty.Register("Minimum", typeof(double), typeof(UC_MultiRangeSlider), new PropertyMetadata(_min));
///
/// 最大值,默认为100
///
public double Maximum
{
get { return (double)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
public static readonly DependencyProperty MaximumProperty =
DependencyProperty.Register("Maximum", typeof(double), typeof(UC_MultiRangeSlider), new PropertyMetadata(_max));
///
/// 选中开始值,默认为0
///
public double StartValue
{
get { return (double)GetValue(StartValueProperty); }
set { SetValue(StartValueProperty, value); }
}
public static readonly DependencyProperty StartValueProperty =
DependencyProperty.Register("StartValue", typeof(double), typeof(UC_MultiRangeSlider));
///
/// 选中结束值,默认为100
///
public double EndValue
{
get { return (double)GetValue(EndValueProperty); }
set { SetValue(EndValueProperty, value); }
}
public static readonly DependencyProperty EndValueProperty =
DependencyProperty.Register("EndValue", typeof(double), typeof(UC_MultiRangeSlider), new PropertyMetadata(_max));
#endregion
#region 前台交互
///
/// 对两个拖动条进行裁剪
///
private void ClipSilder()
{
double selectedValue = EndValue - StartValue;
double totalValue = Maximum - Minimum;
double sliderClipWidth = (double)SilderWidth * (StartValue - Minimum + selectedValue / 2.0) / totalValue;
// 对第一个拖动条进行裁剪
StartRect = new Rect(0, 0, sliderClipWidth, SilderHeight);
// 对第二个拖动条进行裁剪
EndRect = new Rect(sliderClipWidth, 0, SilderWidth, SilderHeight);
}
///
/// 初始化裁剪
///
private void UC_Arrange_Loaded(object sender, RoutedEventArgs e)
{
ClipSilder();
}
private void SL_Bat1_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
{
if (e.NewValue > EndValue) // 检查值范围
StartValue = EndValue; // 超出,重设为最大值
ClipSilder();
}
private void SL_Bat2_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
{
if (e.NewValue < StartValue)
EndValue = StartValue;
ClipSilder();
}
private void TextBox_KeyUp1(object sender, System.Windows.Input.KeyEventArgs e)
{
try
{
if (e.Key == Key.Enter) // 按回车时确认输入
StartValue = Convert.ToInt32(((TextBox)sender).Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void TextBox_KeyUp2(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Enter)
EndValue = Convert.ToInt32(((TextBox)sender).Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
}
}