WPF: Customizing Windows Presentation Foundation controls

Source: Internet
Author: User

Although there are many control models in Windows presentation Foundation, it is not possible to provide each of the controls you need. At this point, control authoring comes in handy. In this article, I'll show you how to customize an existing control using windows®presentation Foundation, and how to create an entirely new control (or element) for your project.

Before you develop a custom control, you should ask yourself if you really need it. In Windows presentation Foundation, the combination, styling, and templating features allow you to customize existing controls, which are unmatched by previous technologies. Before deciding to create a new control, let's quickly talk about the methods of these three custom controls.

Working with combinations

Creating a composite control is a common requirement. A composite control is a control that consists of more than one control. Suppose you have a play button to start the video playback. XAML and controls are shown in Figure 1.

Figure 1 Simple Play controls

<StackPanel>
<button height= "width=" "content=" Play "/> <polygon horizontalalignment=
" Center "
points=" 0,0 0,26 17,13 "
fill=" Black "/>
</StackPanel>

You need to be able to get the play icon and place it on the button. You can use a combination to actually embed a XAML element inside another XAML element. For example, you can create labels and icons (as the contents of the button) by changing the XAML. Place these elements in the container within the button (here is StackPanel) so that they can be assigned to the Content property of the buttons class, as shown in Figure 2. The resulting button works just like any other button, but it has your own content.

All content in the Figure 2 button

<StackPanel>
<button height= "width=" >
<StackPanel>
<textblock>play </TextBlock>
<polygon points= "0,0 0,26 17,13" fill= "Black"/> </stackpanel>
  
    </Button>
</StackPanel>
  

It is easy to create such controls using a combination. Unlike the controls in demonstration technologies such as Windows Forms, Visual basic®6.0, and MFC, most composite controls are containers for other controls. When all you really need is a composite control, you don't have to write a custom control.

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.