silverlight教程(7)——使用控制模板来定制一个控制的外观和感觉

来源:百度文库 编辑:神马文学网 时间:2024/10/04 01:43:05
其中一个最强大的功能的wpf和silverlight规划模型,是能够完全定制的外观和感觉的控制使用,它这允许开发人员和设计人员,以造型用户界面的控制,在双方微妙和具有戏剧性的方式,使大量的灵活性,以创造正是用户体验理想。

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.在本教程部分,我们将看到几个方法,你可以通过定制控制,然后关闭,由抛光了用户界面,我们的digg中应用这些技巧。

Customizing the Content of Controls 定制的内容管制

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部分我们的补习,我们增加了一个简单的按钮控制网页展示了如何建立一个定制的"推我! " text string for its content.  We then wired up a "Click" event handler that executed code when it was clicked:文本字符串的内容。然后,我们可以用电了"点击"事件处理即处死代码时,它被点击:

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.事情之一,有可能突击一下按钮控制的是,它的"内容"的财产并不一定是一个简单的字符串喜欢的"推我! " ,我们其实可以在设置的"内容"的财产,以什么顺序形状或控制我们想要的。

For example, we could embed a StackPanel with an and control within it:举例来说,我们可以嵌入一个stackpanel与控制它:

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):这将导致我们的按钮看起来像下面时有效。指出,它仍保留了相同的功能特性(推它和按钮仍会压低,并通过点击事件处理,将消防像以前) :

We could alternatively use Shape controls (like the Ellipse control below) to create custom vector graphics inside the button.我们可以交替使用形状控制(如椭圆控制以下) ,以创建定制的向量图形内的按钮。

Notice above how I'm filling the Ellipse control with an offset RadialGradientBrush to add a nice reflective polish to it:公告上述如何,我填补椭圆控制抵消radialgradientbrush加入尼斯反射波兰语给它:

We could even get wacky and embed interactive controls like a Calendar control within the Button:我们甚至可以得到怪诞和嵌入互动管制像一个日历控制按钮:

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!).在上述样本日历是完全互动的-即最终用户其实可以一页前进后退通过内,并选择一个日期上的日历,然后将其含有的按钮,启动"点击"事件处理: (注:我百万不知道为什么,这将是一个很好的用户体验-但它确实表明小康的灵活性,您该怎么办! ) 。

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.这些类型的内容,个性化的情况,概述了上述工作不仅是为按钮控制,但同样对于其他所有控制来自contentcontrol基地班。

Customizing Controls using Control Templates 定制控制用控制模板

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让你能更进一步不仅仅是定制党内的内容控制。 It optionally allows you to completely replace a control's visual tree with anything you want - while still keeping the control behavior intact.它可以选择让您完全取代管制的视觉树与任何你想-虽然仍保持控制行为完好无损。

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:比如,我们不妨说,我们不希望我们的按钮有默认的矩形按钮看看,而不是我们希望他们能有一个习俗轮按钮样子如下:

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:我们能做到这一点,创造一个" roundbutton "风格,在我国app.xaml档案。内部,我们将凌驾按钮的"模板"的财产,并提供一个controltemplate取代按钮默认的矩形视觉与椭圆控制和textblock内它:

We can then have a