Use WPF to implement a progress bar with a slider

Source: Internet
Author: User

Background:

There are still too few people using WPF, and there are many discussions on how WPF will develop in the future on the Internet. In short, the definition of WPF is Windows presentationfoundation, which is specifically designed for the presentation layer by Microsoft, that is, the UI. Moreover, the XAML language born with WPF will also have a broader future thanks to Windows 8's vigorous promotion "s, senior vice president of Development Department. somasegar announced that Microsoft's XAML technical team was directly integrated into the windows department ".

I personally think that using WPF can easily make things that are hard to implement in winform, and the effect of WPF also has a greater space to play. Besides, it is convenient to use each other between WPF and winform. The following is a specific example.

The "progress bar with a slider" is to combine the progressbar and the trackbar (slider) so that the progress of the progress bar can be adjusted like the slider. I enter the keyword "progress bar with slider" on Baidu, but I have not found much available content. Therefore, it is very troublesome to implement a UI control using WPF and put it in the winform program for calling (if winform is used directly ).

Code:

First, create a new usercontrol in blend3. The structure is as follows:

Pbar is a progressbar, gxp is a slider, and textblock is used to display percentages. Overlaps the slider on the progressbar and uses the slider to adjust the progress. If no modification is made, the effect is as follows.

We need to hide the line in the slider by editing the slider template. The "Edit template" tool is really easy to use. You can easily modify the default style of the WPF control, and what you see is what you get, which is not possible in winform. Now we can see the internal structure of the slider!

We found that "[border]" is the line to be hidden. We can set it to hidden directly in the attribute. What are the following steps? The appearance is basically done. If you have other appearance requirements, such as the color to be set, you can edit the template. The following steps are used to synchronize the progress value. When you adjust the slider, the progress bar does not respond because the value of the progressbar is not synchronized with the value of the slider.

The method is to bind the binding, which is one of the core of WPF and is indeed very useful and has a lot of space to play. Add:

Value = "{bindingelementname = gxp, Path = value }"

The effect is coming out!

The last step is how to call the WPF control in winform. Add four WPF references (with blend installed), presentationcore, presentationframework, windowsbase, and windowsformsintegration. Then add usingsystem. Windows. Forms. Integration;

Elementhostehost = new elementhost ();

Ehost. width = 152;

Ehost. Height = 21;

M_progressuc = newprogressgxp. maincontrol ();

Ehost. Child = m_progressuc;

Panel1.controls. Add (ehost );

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.