简体   繁体   中英

EventSetter for Slider Control WPF

Tried to write an EventSetter for 4 slide controls but it doesn't work. Keeps giving me error ie 'FourthProject.MainWindow' does not contain a definition for 'slider_ValueChanged' and no extension method 'slider_ValueChanged' accepting a first argument of type 'FourthProject.MainWindow' could be found (are you missing a using directive or an assembly reference?)

This is what I tried:

<EventSetter Event="Slider.ValueChanged"
Handler="slider_ValueChanged" />

Here is the code behind

private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { SolidColorBrush backgroundColor = new SolidColorBrush(); backgroundColor.Color = Color.FromArgb((byte)alphaSlider.Value (byte)redSlider.Value, (byte)greenSlider.Value, (byte)blueSlider.Value);

        // set colorLabel's background to new color
        colorLabel.Background = backgroundColor;
    }

Please help I am new to WPF and its quite confusing. Thanks

XAML is just fine:

<EventSetter Event="Slider.ValueChanged" Handler="Slider_ValueChanged"/>

You just need to properly define your handler (code behind):

public void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    MessageBox.Show("Value Changed: " + e.NewValue.ToString());
}

Maybe you have a DataContext set on your root element or your event handler isn't public.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM