【问题标题】:WPF Listview binding to ItemSource?WPF Listview 绑定到 ItemSsource?
【发布时间】:2011-01-14 08:44:35
【问题描述】:

我有以下列表视图,但它不显示实际记录,而只显示对象的命名空间。我想知道是否需要在 XAML 中创建列以显示记录,然后将其绑定到对象的某些属性,或者这有什么问题?

<ListView
            Name="ListCustomers"
            ItemsSource="{Binding Path=ListOfCustomers}"
            SelectedItem="{Binding Path=SelectedCustomer}"
            SelectionMode="Single"
            IsSynchronizedWithCurrentItem="True"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            MinHeight="100"

            ></ListView>

ListOfCustomersObservableCollection&lt;Customer&gt; 类型。实际客户确实会加载到 ObservableCollection 中,但不会显示。缺少什么?

【问题讨论】:

    标签: c# wpf listview binding


    【解决方案1】:

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

    <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>
    

    【讨论】:

      【解决方案2】:

      你也可以试试

      <ListView
      .
      .
      ItemTemplate="{StaticResource CustomerDataTemplate}"
      .
      .
      />
      

      其中 CustomerDataTemplate 是 Customer 类的 DataTemplate...

      【讨论】:

        【解决方案3】:

        是不是因为你没有用暴露ListOfCustomers属性的实例设置ListView的DataContext属性(返回要显示的项目列表)?

        【讨论】:

        • 我已经将窗口的数据上下文设置为包含该属性的类,这还不够吗?
        • @Tony - 应该是。它应该冒泡以找到数据上下文。似乎您已经从 acc 答案中解决了它。出了什么问题?
        • 问题是我的列表视图中没有创建绑定到我的客户类的列。
        猜你喜欢
        • 2014-12-08
        • 2018-08-02
        • 2015-04-07
        • 1970-01-01
        • 1970-01-01
        • 2012-01-08
        • 2018-03-24
        • 2011-12-17
        • 2011-10-26
        相关资源
        最近更新 更多