首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF Listview绑定到ItemSource?

WPF Listview绑定到ItemSource?
EN

Stack Overflow用户
提问于 2010-02-08 20:51:07
回答 3查看 97.5K关注 0票数 21

我有下面的listview,但是它没有显示实际的记录,而只显示了对象的名称空间。我想知道是否需要在XAML中创建列来显示记录,然后将其绑定到对象的某些属性,或者这样做有什么问题?

代码语言:javascript
复制
<ListView
            Name="ListCustomers"
            ItemsSource="{Binding Path=ListOfCustomers}"
            SelectedItem="{Binding Path=SelectedCustomer}"
            SelectionMode="Single"
            IsSynchronizedWithCurrentItem="True"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            MinHeight="100"

            ></ListView>

ListOfCustomers是一种ObservableCollection<Customer>类型。实际的客户会加载到ObservableCollection中,但不会显示。遗漏了什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-02-08 20:55:51

您还需要选择要显示的列:

代码语言:javascript
复制
<ListView ItemsSource="{Binding ListOfCustomers}"
          SelectedItem="{Binding Path=SelectedCustomer}"
          ....>
  <ListView.View>
    <GridView>
      <GridViewColumn Width="140" Header="First Name"
         DisplayMemberBinding="{Binding FirstName}"  />
      <GridViewColumn Width="140" Header="Last Name"  
         DisplayMemberBinding="{Binding LastName}" />
      <GridViewColumn Width="140" Header="Email Address"
         DisplayMemberBinding="{Binding Email}" />
      ....
    </GridView>
  </ListView.View>
</ListView>
票数 46
EN

Stack Overflow用户

发布于 2010-02-08 20:59:35

您也可以尝试

代码语言:javascript
复制
<ListView
.
.
ItemTemplate="{StaticResource CustomerDataTemplate}"
.
.
/>

其中CustomerDataTemplate是Customer类的DataTemplate ...

票数 4
EN

Stack Overflow用户

发布于 2010-02-08 20:53:47

是否因为您尚未使用公开ListOfCustomers属性(返回要显示的项的列表)的实例设置ListView的DataContext属性?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2221621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档