WPF - 无法获取绑定数据以在 ListView 中显示
我在 gridview 中有一个列表。我已绑定 gridview 列以获取 JFifoData 类中的集合成员,我已将其实例收集在可观察集合中。然后我将 ListView ItemsSource 绑定到该集合。然而,由于某种原因,当我运行程序时,数据没有显示。这是相关代码,我做错了什么吗?
XAML 代码
<ListView Name="JfifoList" ItemsSource="{Binding JFifoCollection}">>
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Time}" Header="time" Width="225"/>
<GridViewColumn DisplayMemberBinding="{Binding FEStatus}" Header="fe status" Width="225"/>
<GridViewColumn DisplayMemberBinding="{Binding BEStatus}" Header="be status" Width="225"/>
<GridViewColumn DisplayMemberBinding="{Binding Trigger}" Header="trigger" Width="350"/>
</GridView>
</ListView.View>
</ListView>
JFifoData 类
public class JFifoData
{
public DateTime Time { get; set; }
public string FEStatus { get; set; }
public string BEStatus { get; set; }
public string Trigger { get; set; }
public uint TID { get; set; }
public uint Frames { get; set; }
public uint HWCRC { get; set; }
public uint FPS { get; set; }
public string Faults { get; set; }
public string Info { get; set; }
public string Config { get; set; }
}
获取我的窗口类的成员
public ObservableCollection<JFifo.JFifoData> JFifoCollection
{
get
{
return Fifo.CollectedData;
}
}
Observable 集合的初始化
Data = new ObservableCollection<JFifoData>();
Data.Add(new JFifoData
{
Time = new DateTime(),
FEStatus = "FE Good",
BEStatus = "BE Good",
Trigger = "Trigged"
});
Data.Add(new JFifoData
{
Time = new DateTime(),
FEStatus = "FE Bad",
BEStatus = "BE Bad",
Trigger = "Not Trigged"
});
I have a list in gridview. I have bound the gridview columns to get set members in a class JFifoData, instances of which I have collected in an Observable Collection. I have then bound the ListView ItemsSource to this collection. For some reason, however, the data is not being displayed when I run the program. Here is the relevant code, am I doing something wrong?
XAML Code
<ListView Name="JfifoList" ItemsSource="{Binding JFifoCollection}">>
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Time}" Header="time" Width="225"/>
<GridViewColumn DisplayMemberBinding="{Binding FEStatus}" Header="fe status" Width="225"/>
<GridViewColumn DisplayMemberBinding="{Binding BEStatus}" Header="be status" Width="225"/>
<GridViewColumn DisplayMemberBinding="{Binding Trigger}" Header="trigger" Width="350"/>
</GridView>
</ListView.View>
</ListView>
JFifoData class
public class JFifoData
{
public DateTime Time { get; set; }
public string FEStatus { get; set; }
public string BEStatus { get; set; }
public string Trigger { get; set; }
public uint TID { get; set; }
public uint Frames { get; set; }
public uint HWCRC { get; set; }
public uint FPS { get; set; }
public string Faults { get; set; }
public string Info { get; set; }
public string Config { get; set; }
}
get member of my window class
public ObservableCollection<JFifo.JFifoData> JFifoCollection
{
get
{
return Fifo.CollectedData;
}
}
Initialization of the Observable Collection
Data = new ObservableCollection<JFifoData>();
Data.Add(new JFifoData
{
Time = new DateTime(),
FEStatus = "FE Good",
BEStatus = "BE Good",
Trigger = "Trigged"
});
Data.Add(new JFifoData
{
Time = new DateTime(),
FEStatus = "FE Bad",
BEStatus = "BE Bad",
Trigger = "Not Trigged"
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码看起来不错,但是,如果这是代码的所有相关部分,则您尚未为
ListView
设置DataContext
。执行以下操作:您的 XAML 也可能格式错误。看第一行:
它的末尾有两个尖括号!
Your code looks fine, however, if this is all the relevant parts of your code, you have not set the
DataContext
for yourListView
. Do the following:It would also appear that your XAML is malformed. Look at the first line:
It has two angle-brackets at the end!
似乎您的
DataContext
设置不正确。运行代码,然后查看 Visual Studio 输出窗口,看看是否出现任何绑定错误。Seems like your
DataContext
isnt set correctly. Run the code and then take a look at Visual Studios output window and see if you get any binding errors.