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 );