【问题标题】:Binding a generic list to listview in wpf将通用列表绑定到 wpf 中的列表视图
【发布时间】:2014-09-15 17:27:53
【问题描述】:

假设我有以下课程。我的意图是将列表绑定到列表视图。这只是一个示例代码。请帮忙。

Class Data
{
public string name {get;set;}
public DateTime date{get;set;}
}

Class Items
{
//Code to get items from database and create a list<Data>...
ListViewDetails.DataContext=List<Data>; //Not Sure if this is the right way...
}

XAML:

 <ListView Name="ListViewDetails" Margin="4,20,40,100" ItemTemplate="{DynamicResource EmployeeTemplate}" ItemsSource="{Binding Path=List}">
    <GridView>

            <GridViewColumn Header="Employee Name" DisplayMemberBinding="{Binding Path=name}"/>

            <GridViewColumn Header="Hire Date" DisplayMemberBinding="{Binding Path=date}"/>
    </GridView>
    </ListView>

【问题讨论】:

  • 感谢您找到示例的想法..

标签: c# wpf listview wpf-4.0


【解决方案1】:

不正确:

  1. 您不想通过代码在控件上设置数据上下文。
  2. 绑定时通常不使用List

改为:

  1. 将整个页面的数据上下文设置为视图模型对象。然后绑定到该对象的属性,而不是为控件设置数据上下文。
  2. 绑定到集合时使用ObservableCollection&lt;T&gt;。如果集合中添加或删除了项目,UI 将自动更新以反映这一点。

【讨论】:

猜你喜欢
  • 2017-09-22
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 2011-12-02
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 2013-11-25
相关资源
最近更新 更多