In this tutorial segment we'll look at a few ways you can customize controls, and then close out by polishing up the user interface of our Digg application using these techniques.在本教程部分,我們將看到幾個(gè)方法,你可以通過(guò)定制控制,然后關(guān)閉,由拋光了用戶界面,我們的digg中應(yīng)用這些技巧。
In Part 1 of our tutorial we added a simple button control to the page and demonstrated how to set a custom "Push Me!"在第1部分我們的補(bǔ)習(xí),我們?cè)黾恿艘粋€(gè)簡(jiǎn)單的按鈕控制網(wǎng)頁(yè)展示了如何建立一個(gè)定制的"推我! " text string for its content. We then wired up a "Click" event handler that executed code when it was clicked:文本字符串的內(nèi)容。然后,我們可以用電了"點(diǎn)擊"事件處理即處死代碼時(shí),它被點(diǎn)擊:
This caused the button to render like below within the browser:這引起了按鈕,使想下面該瀏覽器:
One of the things that might surprise you about the Button control is that its "Content" property does not have to be a simple string like "Push Me!". We can in fact set the "Content" property to be any sequence of Shapes or Controls we want.事情之一,有可能突擊一下按鈕控制的是,它的"內(nèi)容"的財(cái)產(chǎn)并不一定是一個(gè)簡(jiǎn)單的字符串喜歡的"推我! " ,我們其實(shí)可以在設(shè)置的"內(nèi)容"的財(cái)產(chǎn),以什么順序形狀或控制我們想要的。
For example, we could embed a StackPanel with an <Image> and <TextBlock> control within it:舉例來(lái)說(shuō),我們可以嵌入一個(gè)stackpanel與<image>和<textblock>控制它:
This will cause our Button to look like below at runtime. Note that it still retains the same functionality behavior (push it and the button will still depress, and the click event handler will fire like before):這將導(dǎo)致我們的按鈕看起來(lái)像下面時(shí)有效。指出,它仍保留了相同的功能特性(推它和按鈕仍會(huì)壓低,并通過(guò)點(diǎn)擊事件處理,將消防像以前) :
We could alternatively use Shape controls (like the Ellipse control below) to create custom vector graphics inside the button.我們可以交替使用形狀控制(如橢圓控制以下) ,以創(chuàng)建定制的向量圖形內(nèi)的按鈕。
Notice above how I'm filling the Ellipse control with an offset RadialGradientBrush to add a nice reflective polish to it:公告上述如何,我填補(bǔ)橢圓控制抵消radialgradientbrush加入尼斯反射波蘭語(yǔ)給它:
We could even get wacky and embed interactive controls like a Calendar control within the Button:我們甚至可以得到怪誕和嵌入互動(dòng)管制像一個(gè)日歷控制按鈕:
In the above sample the Calendar is fully interactive - meaning end-users can actually page back and forward through the months and choose a date on the Calendar, and then push its containing button to trigger the "Click" event handler: (note: I'm not sure why this would be a good user experience - but it does show off the flexibility of what you can do!).在上述樣本日歷是完全互動(dòng)的-即最終用戶其實(shí)可以一頁(yè)前進(jìn)后退通過(guò)內(nèi),并選擇一個(gè)日期上的日歷,然后將其含有的按鈕,啟動(dòng)"點(diǎn)擊"事件處理: (注:我百萬(wàn)不知道為什么,這將是一個(gè)很好的用戶體驗(yàn)-但它確實(shí)表明小康的靈活性,您該怎么辦! ) 。
These types of content customization scenarios I outlined above work not just for Button controls, but likewise for all other controls that derive from the ContentControl base class.這些類型的內(nèi)容,個(gè)性化的情況,概述了上述工作不僅是為按鈕控制,但同樣對(duì)于其他所有控制來(lái)自contentcontrol基地班。
The control model used by Silverlight and WPF allows you to go much further than just customizing the inner content of a control.控制模型所使用的silverlight和wpf讓你能更進(jìn)一步不僅僅是定制黨內(nèi)的內(nèi)容控制。 It optionally allows you to completely replace a control's visual tree with anything you want - while still keeping the control behavior intact.它可以選擇讓您完全取代管制的視覺(jué)樹(shù)與任何你想-雖然仍保持控制行為完好無(wú)損。
For example, let's say we don't want our buttons to have the default rectangle push button look, and instead we want them to have a custom round button look like below:比如,我們不妨說(shuō),我們不希望我們的按鈕有默認(rèn)的矩形按鈕看看,而不是我們希望他們能有一個(gè)習(xí)俗輪按鈕樣子如下:
We could accomplish this by creating a "RoundButton" style in our App.xaml file. Within it we'll override the Button's "Template" property, and provide a ControlTemplate that replaces the Button's default Rectangle visual with an Ellipse control and a TextBlock inside it:我們能做到這一點(diǎn),創(chuàng)造一個(gè)" roundbutton "風(fēng)格,在我國(guó)app.xaml檔案。內(nèi)部,我們將凌駕按鈕的"模板"的財(cái)產(chǎn),并提供一個(gè)controltemplate取代按鈕默認(rèn)的矩形視覺(jué)與橢圓控制和textblock內(nèi)它:
We can then have a <Button> control reference this Style resource to use this "RoundButton" look and feel:我們便可以有一個(gè)<button>控制參考這種風(fēng)格的資源,利用這一" roundbutton "外觀與感覺(jué):
Incorporating Content within Our Control Templates 包含的內(nèi)容在我們控制的范本
One thing you might have noticed in our "RoundButton" control template above is that the size of the Button, and the Content displayed within it, are hard-coded (it always says "Push Me!").有件事你可能已經(jīng)注意到,在我們的" roundbutton "控制模板以上是大小的按鈕,顯示的內(nèi)容,它內(nèi)部的,是硬編碼(它總是說(shuō), "推我! " ) 。
The good news is that WPF and Silverlight make it possible for us to have these settings be customizable as well. We can do this using a {TemplateBinding ControlProperty } markup extension syntax inside the control template to bind to properties of the control. This will allow our control template to adapt as properties are set on the Control by an external developer:好消息是, wpf和silverlight使我們有可能有這些設(shè)置可定制的,以及我們可以做到這一點(diǎn)用( templatebinding controlproperty )標(biāo)記擴(kuò)展語(yǔ)法內(nèi)部控制模板約束性質(zhì)的控制,這將使我們所能控制的樣板,以適應(yīng)作為屬性是一套關(guān)于控制由外部開(kāi)發(fā)商:
Notice above how instead of adding a <TextBlock> control to display the content, we are instead using the <ContentPresenter> control. That will enable us to have the button display not just text strings, but any custom content as well (just like we did earlier in this tutorial).以上通知,而不是如何加<textblock>控制要顯示的內(nèi)容,我們正在用這一新<contentpresenter>控制,這將使我們能夠有按鈕顯示不是簡(jiǎn)單的文字字串,但任何定制內(nèi)容以及(就像我們較早時(shí)說(shuō),在此教程) 。
We can then use the above Style on three buttons below (each with different content and property settings):我們便可以利用上述方式對(duì)三個(gè)按鈕下面(各有不同的內(nèi)容和財(cái)產(chǎn)設(shè)定) :
The above Buttons will then display like below (and yes - the scaled Calendar control still supports paging and date selection!):上述按鈕,然后將展出像下面(是-規(guī)模日歷控制仍然支持尋呼和日期的選擇! ) :
If we wanted to go further we could optionally add storyboard animations to the ControlTemplate (to handle button states like "hover", "focus" and "pushed"). This capability enables us to create really polished user interaction scenarios, and enable scenarios not possible with HTML.如果我們想要更進(jìn)一步,我們可以有選擇地放入串連動(dòng)畫(huà)向controltemplate (處理按鈕國(guó)家一樣, "懸停" , "焦點(diǎn)"和"推" ) 。這方面的能力,使我們能夠制造出非常拋光用戶交互情景,讓情景不可能與HTML 。
Developers working with controls in an application can remain oblivious to all of this style and control interaction customization. They can handle events off of the controls and manipulate the control's object model as normal, and have a designer own sculpting and customizing the look and feel separately using styles and templates.開(kāi)發(fā)人員的管制,在申請(qǐng)時(shí),可以繼續(xù)無(wú)視這一切,作風(fēng)和控制互動(dòng)定制,他們可以處理的事件小康的控制和操縱控制的對(duì)象模型作為正常的,有一個(gè)設(shè)計(jì)師自己的造型和定制的外觀和感覺(jué)分開(kāi)用風(fēng)格和模板。
Now that we've covered some of the basics of how Control Templates work, let's use them in a few places to add a bit more UI polish to our Digg application.現(xiàn)在,我們已涵蓋的一些基本知識(shí),如何控制模板工作,讓它們用在少數(shù)地方,以增添多一點(diǎn)的UI波蘭語(yǔ)我們digg申請(qǐng)。
Right now there is one obvious place in the application that clearly needs some work - the "Close" button on our User Control:現(xiàn)在有一個(gè)很明顯的地方,在應(yīng)用這顯然需要一定的工作-"關(guān)閉"按鈕,我們的用戶控制:
The good news is that this is easy for either us (or a designer working with us) to fix. We could add a ControlTemplate to the "CloseButton" style in our App.xaml file and add some custom vector shapes to provide a more attractive close button (note: a more competent designer than me could also add hover and animation behavior to the vector graphic shapes and make it look even better):好消息是,這是很容易要么我們(或設(shè)計(jì)師與我們共事等)確定,我們可以添加controltemplate向" closebutton "風(fēng)格,在我國(guó)app.xaml檔案,并添加一些定制矢量形狀,以提供更具吸引力關(guān)閉按鈕(注意:一個(gè)更稱職的設(shè)計(jì)師比我還可以放入懸停和動(dòng)畫(huà)行為,以矢量圖形的形狀,使之期待,甚至更好) :
When we run the application again the button will now look like it does below:當(dāng)我們運(yùn)行應(yīng)用程序再次按鈕,將現(xiàn)在看似但這如下:
The second place in our application that I think we should work to polish up more is the outer UI of our ListBox. If you look closely at it you can see that the ListBox in Beta1 has a nested border look as its default (note: we are still finalizing the default skins we ship with so this will likely change before the final release):第二次發(fā)生在我國(guó)的應(yīng)用,我認(rèn)為我們應(yīng)該致力于波蘭語(yǔ)了,更重要的是外層的UI我們的列表,如果你仔細(xì)看看它,你可以看到,該列表在素beta1有一個(gè)嵌套邊界看作為其默認(rèn)(注:我們?nèi)栽谇枚J(rèn)皮膚,我們船,所以這可能會(huì)改變之前的最后一份新聞稿) :
We can remove this and go with a flat border around the ListBox by customizing its Control Template. Below is a ListBox style with a custom template that does this:我們可以刪除此去同一個(gè)單位邊界周圍的列表,由用戶自己控制模板,下面是一個(gè)列表風(fēng)格定制模板,這是否:
Notice how we've removed all border controls from the ListBox. We are now just using the <ScrollViewer> control in Silverlight (which enables scrolling of any content within it) and embedding into it an <ItemsPresenter/> control which will own rendering the actual items in the ListBox (it will use the <DataTemplate> we created in Part 4 to render these items).公告如何,我們已經(jīng)取消了所有的邊界管制,從列表,現(xiàn)在我們只用了<scrollviewer>控制在silverlight (使?jié)L動(dòng)的任何內(nèi)容) ,并嵌入到它的一個(gè)<itemspresenter/>控制,將自己繪制了實(shí)際項(xiàng)目中的列表框(它會(huì)利用<datatemplate>我們創(chuàng)造了在第4部分,以使這些項(xiàng)目) 。
Below is the more flat look this now gives us for our List:下面是更多的單位看看這個(gè),現(xiàn)在讓我們?yōu)槲覀兊拿麊危?/span>
What is cool is that we did not have to change any code in our application, nor modify the XAML markup on our actual controls, in order to make these look and feel changes. This code/design separation enables a great developer and designer workflow when working on Silverlight and WPF applications. Expression Blend and the overall Expression Studio products take these control design capabilities to the next level, and will deliver a rich designer toolset that makes this customization easy.什么是酷的是,我們沒(méi)有改變?nèi)魏未a,在我們的申請(qǐng),也沒(méi)有修改xaml標(biāo)記對(duì)我們的實(shí)際控制,為了使這些外觀與感受的變化,這代碼/設(shè)計(jì)分離,使一個(gè)偉大的開(kāi)發(fā)商和設(shè)計(jì)師的工作流程,當(dāng)工作silverlight和wpf申請(qǐng)。表達(dá)配方和整體表達(dá)Studio產(chǎn)品采取這些控制設(shè)計(jì)能力,到新的層次,并會(huì)提供了豐富的設(shè)計(jì)工具,使這一定制容易。
聯(lián)系客服