/// §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ /// <summary> /// After the slider value has changed, update the Value property to /// hold the scaled value. /// </summary> private static void SetValueToTick(SliderMenuItem item, double tickValue) { if (item.m_TickValueMap.ContainsKey(tickValue)) { item.Value = item.m_TickValueMap[tickValue]; return; } double[] keys = new double[item.m_TickValueMap.Keys.Count]; item.m_TickValueMap.Keys.CopyTo(keys, 0); int index = Array.BinarySearch <double>(keys, tickValue); Debug.Assert(index < 0, "What? How come I didn't find the key already?"); index = ~index; Debug.Assert(index < item.Items.Count, "How did tick value go above 1000?"); Debug.Assert(index != 0, "Insert location was before element 0."); double lowTick = keys[index - 1]; double highTick = keys[index]; double lowValue = item.m_TickValueMap[lowTick]; double highValue = item.m_TickValueMap[highTick]; double valueScale = highValue - lowValue; double sourceScale = highTick - lowTick; double newValue = (tickValue - lowTick) * valueScale / sourceScale + lowValue; item.Value = newValue; }
/// §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ /// <summary> /// Listen for change to Value property. /// </summary> /// <param name="obj"></param> /// <param name="e"></param> private static void Value_ValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { // find appropriate tick spot, and set slider value SliderMenuItem item = obj as SliderMenuItem; if (item != null) { SetTickToValue(item, (double)e.NewValue); } }
/// §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ /// <summary> /// Sets the slider thumb to the closest match after the value changes. /// </summary> /// <param name="item"></param> /// <param name="newValue"></param> private static void SetTickToValue(SliderMenuItem item, double newValue) { // find tick spot where double[] ticks = new double[item.m_TickValueMap.Keys.Count]; item.m_TickValueMap.Keys.CopyTo(ticks, 0); // Find exact match if (item.m_TickValueMap.ContainsValue(newValue)) { foreach (double tick in item.m_TickValueMap.Keys) { if (item.m_TickValueMap[tick] == newValue) { item.m_Slider.Value = tick; return; } } } // Find closest match for (int i = 1; i < item.m_TickValueMap.Count; i++) { double lowTick = ticks[i - 1]; double highTick = ticks[i]; double lowValue = item.m_TickValueMap[lowTick]; double highValue = item.m_TickValueMap[highTick]; //double newValue = (double)e.NewValue; if (newValue > lowValue && newValue < highValue) { double valueScale = highValue - lowValue; double tickScale = highTick - lowTick; // set slider to closest tick match double newTick = (newValue - lowValue) / (valueScale) * tickScale + lowTick; item.m_Slider.Value = newTick; } } }