How to correctly handle Slider value change events in WPF, wpfslider

Source: Internet
Author: User

How to correctly handle Slider value change events in WPF, wpfslider

Recently, when I used the WPF Data Binding to rewrite the player project, the Slider control was used to adjust the form transparency and playback progress. To adjust the transparency of a form, you can bind the Value of the Opacity attribute of the form to the Value of the Slider. Opacity = "{Binding ElementName = Slider, Path = Value, mode = OneWay} ", so there is no need to deal with the Slider value change event (ValueChanged ). But I want to save the settings, so I must record the value of the Slider. So I will handle the ValueChanged event, record the value of the Slider in the configuration, and exit the program to save the configuration, start the program to read the configuration, and then assign the saved Value to the Value of Slider. I found that I was naive and gave the Value, but the Slider did not change at all. debugging found that the Value was indeed saved to the configuration, that is, the root cause was that the saved Value was not successfully assigned to the Value, why? I have never figured it out.

On the other hand, when adjusting the progress, I found that ValueChanged is not feasible at all, because the player's playback progress is assigned to the Value of Slider every second, so that a ValueChanged event is triggered every second, the ValueChanged event handler of the Slider in the playback progress writes the code for adjusting the playback progress (that is, where the Slider is located, it plays the media segment, those who have made music players will surely understand). This clearly forms an endless loop !!

After thinking about it, you should not handle the Slider's ValueChanged event. Instead, you should handle it only when you manually adjust the value of the Slider, that is, you should handle the Slider's MouseLeftButtonUp event !!

I found that I was naive again. Nima debugging found that when I manually pull the Slider and then release the mouse, the MouseLeftButtonUp event was not triggered at all, baidu said that the Slider is composed of RepeatButton and Thumb. When you adjust the Slider, The MouseLeftButtonUp of the RepeatButton will be set first, and then e. handled is set to true, that is, the MouseLeftButtonUp of the subsequent Slider is ignored (marked as Handled), so you will never be able to process the MouseLeftButtonUp event of the Slider. What should we do? WPF has a omnipotent method to add Event Handlers: AddHandler. Example: Slider1.AddHandler (Slider. mouseLeftButtonUp, new MouseButtonEventHandler (slider?mouseleftbuttonup), true), the third parameter specifies whether or not it is marked as handled (e. handled = true) All MouseLeftButtonUp events must be Handled. Now the problem is solved ~

In conclusion, do not process the Slider's ValueChanged or MouseLeftButtonUp event in XAML. The correct way to handle the Slider value change event is to use AddHandler to process the Slider's MouseLeftButtonUp event after the form is Loaded. As for the first question, Baidu has arrived again, saying that after the Slider is initialized (Value assignment), an exception will occur when the ValueChanged event is processed. This is a BUG!


In WPF, how does one set the value of the slider control to an integer?

The value on this control is a floating point number by default. You can drag the slider to change its value, use the keyboard when it has a focus, or click a blank area on both sides of the slider (similar to the scroll bar ), the value step has two attributes: LargeChange and SmallChange. The default values are 1 and 0.1 respectively. Therefore, 1.3543 may occur. However, you can customize the values by adding the Code:
Ticks = "1 2 3 4 5" // This is the step Value Setting
IsSnapToTickEnabled = "True" // This statement must be added to ensure the effect.

Try it. It's an integer now. Baidu Hi (do not use QQ)

Wpf uses slider to change the alpha value of the Canvas background

<Canvas x: Name = "ca"
Background = "# FF0000FF"
Width = "300"
Height = "300"
VerticalAlignment = "Center"
HorizontalAlignment = "Center">
<Slider Width = "100"
Height = "30"
Value = "{Binding ElementName = ca, Path = Background. Opacity, Mode = OneWayToSource }"
Canvas. Top = "135"
Canvas. Left = "100"
Minimum = "0"
Maximum = "1"/>
</Canvas>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.