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

发布于 2024-11-29 17:52:47 字数 1858 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦欢 2024-12-06 17:52:47

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

JfifoList.DataContext = theClassWhichExposesJFifoCollection

您的 XAML 也可能格式错误。看第一行:

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

它的末尾有两个尖括号!

Your code looks fine, however, if this is all the relevant parts of your code, you have not set the DataContext for your ListView. Do the following:

JfifoList.DataContext = theClassWhichExposesJFifoCollection

It would also appear that your XAML is malformed. Look at the first line:

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

It has two angle-brackets at the end!

神爱温柔 2024-12-06 17:52:47

似乎您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文