【发布时间】:2013-06-04 22:26:38
【问题描述】:
我正在尝试使用 WPF 和 C# 在 ListView 中显示数据,但我对所看到的不同示例和方法感到困惑。我正在寻找一个与我的程序类似的完整工作示例,或使其工作的先决条件列表。如果我设法从我的集合中只显示 1 行数据,我会很高兴。目前,列表视图不显示任何内容。
C#:
public partial class MainWindow : Window
{
public ObservableCollection<Row> Rows { get; set; }
public MainWindow()
{
InitializeComponent();
Rows = new ObservableCollection<Row>();
Rows.Add(new Row
{
ID = "42",
Category = "cat",
CharLimit = 32,
Text = "Bonjour"
});
}
}
public class Row
{
public string ID { get; set; }
public string Category { get; set; }
public int CharLimit { get; set; }
public string Text { get; set; }
}
XAML:
<ListView ItemsSource="{Binding Path=Rows}">
<ListView.View>
<GridView>
<GridViewColumn Width="200" Header="ID" DisplayMemberBinding="{Binding Path=ID}" />
<GridViewColumn Width="200" Header="Category" DisplayMemberBinding="{Binding Path=Category}" />
<GridViewColumn Width="200" Header="Text" DisplayMemberBinding="{Binding Path=Text}" />
</GridView>
</ListView.View>
</ListView>
提前致谢
【问题讨论】:
-
看起来您没有在任何地方设置 DataContext,在这种情况下应该是 MainWindow 的实例。我还建议研究 MVVM 设计模式和 MVVM 框架。
-
谢谢,我会查看一些 MVVM 教程来掌握这个想法并尝试正确地做 :)
标签: c# wpf listview data-binding binding