Introduction to WPF Templates (Introduction to WPF Templates)

Source: Internet
Author: User

Introduction(Introduction)
Windows Presentation Foundation allows the developer to completely, the look and feel of the controls. This was accomplished by using Control Templates. It means you can render your Button as a Ellipse which when hovered'll change its color. Actually, that's exactly what we were going to does in this article.
(WPF enables developers to radically change the look and feel of controls.) This is done by using a control template. This means that you can make your button oval and change its color when the mouse hovers over it. In fact, this is exactly what we are going to do with this article. )

Why Control Templates and why not Styles?
(Why is it a template instead of a style?) )
In one of the previous articles, we talked about Styles in WPF. You can check out the article:introduction to styling in Windows Presentation Foundation.
(In the previous article, we talked about the styles in WPF.) You can check out this article: Introduction to Styles in WPF. )

One question you might ask is, and the use styles to change the look of the controls. Styles can change the appearance of your control but they'll be a dependent on the properties provided by the control Itse Lf. It means you are not being able to render your Button as a Polygon. Control Templates allow changing the look of the control by providing a template which would be used by the control.
(One question you might ask is why you don't use styles to change the appearance of a control.) Styles can change the appearance of your controls, but they will depend on the features provided by the control itself. This means that you will not be able to make the polygon your button. The control template allows the appearance to be controlled by providing controls that will be used by the control template.

Creating a Roundbutton (creates a circular pushbutton)
In this article, we is going to the Create a round button by using control templates. The first task is to create a simple button control. Here's the code for:
(In this article, we'll use the control template to create a circular button.) The first task is to create a simple button control. Here is the code:)

<Button Content="Push Me!" >

This would create a very simple button control on the WPF form. Let's create a template for the This button. We'll place the template in the App.xaml file so, we can use it throughout the application.
(This creates a very simple button control on a WPF form.) Let's create a template for this button. We will be in the template of the App.xaml file so that we can use it throughout the application. )

<ControlTemplate x:key="Buttontemplate" TargetType="{x:type button } ">              <Grid>           <Ellipse Name="El1" Fill= "Orange" Width="100"  Height="> "                       </Ellipse>                                             </Grid> </ControlTemplate> 

The control template defined above is really simple! First a unique key "Buttontemplate" is assigned to the control template. Also, the TargetType of the template is set to "button" which means this template can have only been applied to a button control. Next, we define an Ellipse inside the Grid control. It's a simple Ellipse filled with orange color.
(The control template defined above is very simple!) The first unique key "Buttontemplate" is assigned to the control template. In addition, the template's targettype is set to "button", which means that the template can only be applied to a button control. Next, we define the ellipse in the grid control. This is a simple oval filled with orange. )


Now, let's apply the template to the Button control:
(now let's apply the template to the button control:)

<Button Content="Push Me!" Template="{StaticResource buttonTemplate}"     Click="Button_Clicked"></Button>

As soon as you apply the control template, the Button would change its display and would be rendered as an Ellipse as shown Below
(as soon as you apply the control template, the button will change its display and will render as an ellipse as shown:)

There is one problem with the above rendering; The content of the button control which is ' Push me! ' is not displayed. Let's make it appear inside the Ellipse. The ContentPresenter control can be used to display the content of a WPF control.
(There is a problem with the rendering above; the contents of the button are not displayed.) Let's make it appear inside the ellipse. The ContentPresenter control can be used to display the contents of a WPF control. )

<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"     Content="{TemplateBinding Button.Content}" />

And here is the result:
(This is the result:)

We is not doing yet! Let's also add a trigger to the Button control so that it changes the color of the Ellipse once the mouse are over it.
(We're not done yet!) We also added a trigger event for the button control, and once the mouse moves to it, the color of the ellipse changes. )

<ControlTemplate.Triggers>          <TriggerProperty="Button.IsMouseOver"Value="True">            <Setter TargetName="el1"Property="Fill"Value="Yellow"/>          </Trigger>        </ControlTemplate.Triggers>

The trigger is fired on the Button.ismouseover event. The Setter is used to change the Fill property of the Ellipse to "Yellow". Now if you are hover over the Ellipse, it'll change from orange to yellow.
(the trigger is triggered in the Button.ismouseover event.) The Fill property used by the set to change the ellipse to "yellow". Now, when you hover over the ellipse, it will change from orange to yellow. )

Conclusion(summary)
WPF Control Template is a very important feature of the WPF Framework. It allows the look and feel of the WPF controls and render them in completely different on from their defau LT format.
(WPF control templates are a very important feature of the WPF framework.) It allows you to change the look and feel of it, making them completely different from the default format. )

Introduction to WPF Templates (Introduction to WPF Templates)

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.