標籤:templates wpf
Introduction(簡介)
Windows Presentation Foundation allows the developer to completely change the look and feel of the controls. This is accomplished by using Control Templates. It means you can render your Button as an Ellipse which when hovered will change its color. Actually, that is exactly what we are going to do in this article.
(WPF使開發人員可以徹底改變控制項的外觀和感覺。這是通過使用控制模板完成。這意味著你可以使你的按鈕為橢圓形,當滑鼠懸浮上的時候改變其顏色。其實,這正是我們要做的這篇文章。)
Why Control Templates and Why Not Styles?
(為什麼是模板而不是樣式呢?)
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.
(在前面的文章中,我們談到了在WPF樣式。您可以檢查出這篇文章:WPF中樣式簡介。)
One question you might ask is why not use styles to change the look of the controls. Styles can change the appearance of your control but they will be dependent on the properties provided by the control itself. It means you will not be able to render your Button as a Polygon. Control Templates allow changing the look of the control by providing a template which will be used by the control.
(你可能會問的一個問題是,為什麼不使用樣式來更改控制項的外觀。樣式可以改變你的控制項的外觀,但他們將取決於由控制本身所提供的特性。這意味著你將無法使多邊形作為你的按鈕。控制模板允許通過提供將被用於由控制模板改變控制的外觀。
Creating a Round Button(創造一個圓形按鈕)
In this article, we are going to create a round button by using control templates. The first task is to create a simple button control. Here is the code for that:
(在這篇文章中,我們將使用控制項範本來建立一個圓形按鈕。第一個任務是建立一個簡單的按鈕控制項。這裡是該代碼:)
<Button Content="Push Me!" >
This will create a very simple button control on the WPF form. Let’s create a template for this button. We will place the template in the App.xaml file so that we can use it throughout the application.
(這將建立WPF表單上的一個非常簡單的按鈕控制項。讓我們來建立此按鈕的模板。我們將在App.xaml檔案的模板,這樣我們就可以在整個應用程式中使用它。)
<ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type Button}"> <Grid> <Ellipse Name="el1" Fill="Orange" Width="100" Height="100"> </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 only be applied to a Button control. Next, we define an Ellipse inside the Grid control. It is a simple Ellipse filled with orange color.
(上面定義的控制項範本是非常簡單的!第一唯一密鑰“buttonTemplate”被分配給控制模板。另外,該模板的TargetType設定為“按鈕”,這意味著該模板僅可應用到一個按鈕控制項。接下來,我們定義網格控制項中的橢圓。這是一個簡單的橢圓填充橙色。)
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 will change its display and will be rendered as an Ellipse as shown below:
(只要你應用了控制模板,該按鈕將更改其顯示和如所示將呈現為一個橢圓:)
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.
(對於上述的渲染有一個問題;按鈕中的內容沒有被顯示。讓我們把它出現在橢圓內。該ContentPresenter控制項可以用來顯示一個WPF控制項的內容。)
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Button.Content}" />
And here is the result:
(這就是結果:)
We are not done yet! Let’s also add a trigger to the Button control so that it changes the color of the Ellipse once the mouse is over it.
(我們還沒有完成!我們還為按鈕控制項添加了一個觸發事件,一旦滑鼠移動到它,橢圓的顏色就會改變。)
<ControlTemplate.Triggers> <Trigger Property="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, when you hover over the Ellipse, it will change from orange to yellow.
(在Button.IsMouseOver事件中觸發器被觸發。設定器用來改變橢圓到“黃色”的填充屬性。現在,當你在橢圓懸停,它會改變從橙黃色。)
Conclusion(總結)
WPF Control Template is a very important feature of the WPF Framework. It allows you to change the look and feel of the WPF controls and render them in completely different way from their default format.
(WPF控制項範本是WPF架構的一個非常重要的特點。它可以讓你改變它的外表和感覺,使它們與預設格式完全的不同。)
Introduction to WPF Templates(WPF模板簡介)