星期二, 十二月 26, 2006

九月PDC新的特性:DisplayAMemberPath

九月PDC新的特性:DisplayAMemberPath


原文出处:http://www.beacosta.com/Archive/2005_10_01_bcosta_archive.html 就像我以前的帖子所表现的,绑定一个ItemsControl到一个IEnumerable数据源确实是很容易(记住ListBox和ComboBox继承自ItemsControl).当你想以文字形式显示每个数据项的单个属性时,利用九月PDC介绍的DisplayMemberPath,就更容易了.在DisplayMemberPath出现之前,这种情形需要使用到DataTemplate来指定我们所兴趣的属性.就像这样的xaml:

    <Window.Resources>
        <DataTemplate x:Key="itemTemplate">
            <TextBlock Text="{Binding Path=Name}" />
        </DataTemplate>
    </Window.Resources>

    <ItemsControl ItemsSource="{StaticResource greekGods}" ItemTemplate="{StaticResource itemTemplate}" />

数据绑定团队意识到这是一个非常常见的情形,需要简单化.这就是产生ItemsControl的DisplayMemberPath属性的缘故了.上面的这种情形现在可以用一行的xaml做到:

    <ItemsControl ItemsSource="{StaticResource greekGods}" DisplayMemberPath="Name" />

很容易吧:)

下面的图片显示了两种版本的ItemsControl,左边的使用DataTemplate,右边的使用DisplayMemberPath.


以下不是翻译内容:
奇怪的是作者的例子显示出来的并不是这样图片.修改以下.:)

share your files at box.net

没有评论: