I have a slider, and I’m updating the value internally. But also, user input is accepted, to change an internal parameter.
The question is: how can I know who raised the event, the user or the class where I change slider.value?
In my case, this is the handler:
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var slider = sender as Slider;
if (slider == null) return;
var col = this.SelectedColor;
switch ((string)slider.Tag)
{
case "Hue": this.SetHue(ValueHue.Value); break;
case "Sat": this.SetSatBri(ValueSat.Value, CurrentBri); break;
case "Bri": this.SetSatBri(CurrentSat, ValueBri.Value); break;
case "R": this.SetColor(Color.FromRgb(Convert.ToByte(ValueR.Value), col.G, col.B)); break;
case "G": this.SetColor(Color.FromRgb(col.R, Convert.ToByte(ValueG.Value), col.B)); break;
case "B": this.SetColor(Color.FromRgb(col.R, col.G, Convert.ToByte(ValueB.Value))); break;
}
}
The function doesn’t work correctly if the event wasn’t raised by user input. So how can I find that out?
You cannot determine how an event has been raised, so you are going to have to take a different approach. Probably the most common solution to this problem is to set a boolean field that indicates that the application is reacting to a specific state change. For example, when you update the value internally:
You can then alter the logic in your question based on the state of the _settingValue variable.