WPF - 无法获取要在ListView中显示的绑定数据

时间:2011-08-15 10:17:33

标签: c# wpf xaml listview observablecollection

我在gridview中有一个列表。我绑定了gridview列以获取JFifoData类中的set成员,我在Observable Collection中收集了这些实例。然后我将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 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"
});

2 个答案:

答案 0 :(得分:1)

好像您的DataContext设置不正确。运行代码,然后查看Visual Studios输出窗口,看看是否有任何绑定错误。

答案 1 :(得分:1)

您的代码看起来不错,但是,如果这是您代码的所有相关部分,则表示您尚未为DataContext设置ListView。执行以下操作:

JfifoList.DataContext = theClassWhichExposesJFifoCollection

您的XAML似乎也会出现格式错误。看第一行:

<ListView Name="JfifoList" ItemsSource="{Binding JFifoCollection}">>

最后有两个尖括号!