分享

silverlight教程(5)——使用列表,并和数据绑定,以显示列表数据

 莣ヤㄋ莪 2009-05-07
以前我们一直在用DataGrid控件,以显示我们的digg故事,这个工程很大,当我们要展示的内容,在一栏格式为我们的digg申请,虽然,我们可能要调整出现多一点,有它少看像一个在DataGrid的故事,而更像是名单当中。好消息是,这很容易-它不要求我们改变我们的任何应用程序代码,以完成这项工作。

We'll start by replacing our DataGrid control with a <ListBox> control.  We'll keep the control name the same as before ("StoriesList"):我们也将开始取代我们的DataGrid控件与<listbox>控制,我们将继续控制名字一样( " storieslist " ) :

When we run our application again and search for stories, the ListBox will display the following results:当我们来说,我们的应用程序,再寻找的故事,列表将显示下列结果:

You might be wondering - why is each item "DiggSample.DiggStory"?  The reason for this is because we are binding DiggStory objects to the ListBox (and the default behavior is to call ToString() on them).  If we want to display the "Title" property of the DiggStory object instead, we can set the "DisplayMemberPath" property on the ListBox:你可能不知道-为什么每个项目的" d iggsample.diggstory" ?究其原因,是因为我们双方都具有约束力, d iggstory物体向列表(及违约行为是呼吁t ostring( ) ,对他们的) ,但如果我们想要展示"标题"财产的安全diggstory对象相反,我们可以设置" displaymemberpath "财产上的列表框:

When we do this the Title will be what is displayed in the ListBox:当我们这样做标题,将是什么显示在列表:

If we want to show more than one value at a time, or customize the layout of each item more, we can override the ListBox control's ItemTemplate and supply a custom DataTemplate.如果我们想表达一个以上的价值,在一个时间,或个性化布局的每一个项目,我们更可以凌驾列表控制的itemtemplate和供应定制datatemplate 。 Within this DataTemplate we can customize how each DiggStory object is displayed.在这个datatemplate我们可以定制如何使每个diggstory对象显示出来。

For example, we could display both the DiggStory Title and NumDiggs value using a DataTemplate like below.举例来说,我们可以显示两diggstory标题和numdiggs价值用datatemplate像下面。

We can databind any public properties we want from our DiggStory object within the DataTemplate.  Notice above how we are using the {Binding PropertyName } syntax to accomplish this with the two TextBlock controls.我们可以databind任何公共财产,我们希望从我们的diggstory物体内部datatemplate 。预告以上我们如何使用(约束力propertyname )语法要做到这一点,与两个textblock管制。

With the above DataTemplate in place, our ListBox will now display its items like below:符合上述datatemplate到位,我们将列表框,现在展现自己的项目,例如如下:

Let's then go one step further and change our DataTemplate to the one below.  This DataTemplate uses two StackPanels - one to stack row items horizontally, and one to stack some textblocks together vertically:让的,然后走一步,进一步转变我们的datatemplate ,将其中的下面,这datatemplate使用两个stackpanels -一至栈连续横向项目,其中以堆叠一些t extblocks一道纵向:

The above DataTemplate causes our ListBox to display items like the screen-shot below:上述datatemplate使我们的列表中显示的项目,如在屏幕上拍摄如下:

when we define the following Style rules in our App.xaml (note how we are using a LinearGradientBrush to get the nice yellow gradient background on the DiggPanel):当我们确定了以下规则作风,在我们的app.xaml (注如何,我们用的是lineargradientbrush得到尼斯黄色梯度背景就diggpanel ) :

One important thing to notice about our ListBox - even though we have customized what the items in it look like, it still automatically provides support for hover and item selection semantics.  This is true both when using the mouse and when using the keyboard (up/down arrow keys, home/end, etc):其中重要一点,以通知我们列表-即使我们定制的是什么项目,它看起来像,它仍然会自动提供支持悬停和项目选择的语义,这才是真正的既当使用鼠标时,使用快捷键( /向下箭头键,主页/结束等) :

The ListBox also supports full flow resizing - and will provide automatic scrolling of our custom content when necessary (notice how the horizontal scroll bar appears as the window gets smaller):该列表还支持全流量大小-并会自动提供滚动的定制内容,必要时(预告如何横向卷轴似乎为窗口减小) :

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多