Silverlight控件-Slider
本篇主要分享关于Slider应用上的一点问题, 相信你看完演示后,已经知道本文的意图了.
在实际开发中,我们需要Slider根据我们设置的SmallChange进行ValueChange.SDK的Slider即使设置了SmallChange属性,
但是在拖动Thumb时,仍会显示实际的Value.显然不能够满足我们的需求,这就需要我们手动去修改Slider.
我相信最初使用Slider的人会和我遇到一样的问题,希望本篇的做法能提供给各位一些思路.
我的做法重写OnValueChanged事件,直接贴出改造后的代码:
public class RoundSlider : Slider, INotifyPropertyChanged
{ public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }}
public double RoundValue
{ get { return (SmallChange == 0 ? Value : Math.Round(Value / SmallChange) * SmallChange); }}
bool _busy=false;
double _discreteValue;protected override void OnValueChanged(double oldValue, double newValue)
{ if (!_busy) {_busy = true;
if (SmallChange != 0) { double newDiscreteValue = Math.Round(newValue / SmallChange) * SmallChange; if (newDiscreteValue != _discreteValue) { Value = newDiscreteValue; base.OnValueChanged(_discreteValue, newDiscreteValue); _discreteValue = newDiscreteValue; }}
else { base.OnValueChanged(oldValue, newValue); }_busy = false;
}NotifyPropertyChanged("RoundValue");
}}
通过RoundValue属性得到我们期望的结果,具体的逻辑也很简单,希望本篇能提供给需要的朋友小小的帮助.

浙公网安备 33010602011771号