WPF - Cannot get Bound Data to display in ListView
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 Co开发者_StackOverflow社区llection
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"
});
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!
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.
精彩评论