猿问

UWP Community Toolkit DataGrid 不显示数据

我正在尝试使用来自 ObservableCollection 的数据在 UWP(来自社区工具包包)中加载 DataGrid。显示了从中读取数据的 CSV 文件的标题,但由于某种原因,没有显示任何数据行。我已经通读并尝试了至少 5 或 6 个关于 SO 的问题,所以虽然我知道之前有人问过这个问题,但这些答案似乎没有用。

在这一点上,我对自己做错了什么感到茫然。我知道我的代码中有一些错误(很明显,否则它会起作用),但我似乎无法找到它。有人可以看看下面的代码,看看他们是否能发现任何错误吗?

注意:我知道数据正在被正确读取,因为 a) 标头显示出来,并且 b) 我遇到了断点并查看了 SpellBook 中的数据,它包含 408 个项目。

在此先感谢任何可以提供帮助的人!


ibeautiful
浏览 113回答 1
1回答

尚方宝剑之说

根据您的要求,我简化了绑定步骤。请检查以下代码。Xaml代码<controls:DataGrid&nbsp; &nbsp; x:Name="MyDataGrid"&nbsp; &nbsp; HorizontalAlignment="Stretch"&nbsp; &nbsp; VerticalAlignment="Stretch"&nbsp; &nbsp; AlternatingRowBackground="Transparent"&nbsp; &nbsp; AlternatingRowForeground="Gray"&nbsp; &nbsp; AreRowDetailsFrozen="False"&nbsp; &nbsp; AreRowGroupHeadersFrozen="True"&nbsp; &nbsp; AutoGenerateColumns="False"&nbsp; &nbsp; CanUserReorderColumns="True"&nbsp; &nbsp; CanUserResizeColumns="True"&nbsp; &nbsp; CanUserSortColumns="False"&nbsp; &nbsp; ColumnHeaderHeight="32"&nbsp; &nbsp; FrozenColumnCount="0"&nbsp; &nbsp; GridLinesVisibility="None"&nbsp; &nbsp; HeadersVisibility="Column"&nbsp; &nbsp; HorizontalScrollBarVisibility="Visible"&nbsp; &nbsp; IsReadOnly="False"&nbsp; &nbsp; Loaded="DataGrid_Loaded"&nbsp; &nbsp; MaxColumnWidth="400"&nbsp; &nbsp; RowDetailsVisibilityMode="Collapsed"&nbsp; &nbsp; RowGroupHeaderPropertyNameAlternative="Range"&nbsp; &nbsp; SelectionMode="Extended"&nbsp; &nbsp; VerticalScrollBarVisibility="Visible"&nbsp; &nbsp; >&nbsp; &nbsp; <controls:DataGrid.RowGroupHeaderStyles>&nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="controls:DataGridRowGroupHeader">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Background" Value="LightGray" />&nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; </controls:DataGrid.RowGroupHeaderStyles>&nbsp; &nbsp; <controls:DataGrid.Columns>&nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTextColumn&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Binding="{Binding Id}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Header="Id"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tag="Id"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTextColumn&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Binding="{Binding Title}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Header="Title"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tag="Title"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridComboBoxColumn&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Binding="{Binding Link}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Header="Link"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tag="Link"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTextColumn&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Binding="{Binding Type}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Header="Type"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tag="Type"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTextColumn&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Binding="{Binding Remark}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Header="Remark"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tag="Remark"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTextColumn&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Binding="{Binding Time}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Header="Time"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tag="Time"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; </controls:DataGrid.Columns></controls:DataGrid>模型类public class Item{&nbsp; &nbsp; public string Id { get; set; }&nbsp; &nbsp; public string Title { get; set; }&nbsp; &nbsp; public string Link { get; set; }&nbsp; &nbsp; public string Type { get; set; }&nbsp; &nbsp; public string Remark { get; set; }&nbsp; &nbsp; public string Time { get; set; }}数据处理private ObservableCollection<Item> Items;private void DataGrid_Loaded(object sender, RoutedEventArgs e){&nbsp; &nbsp; using (var reader = new StreamReader("Assets\\Archive.csv",true))&nbsp; &nbsp; using (var csv = new CsvReader(reader))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var records = csv.GetRecords<Item>();&nbsp; &nbsp; &nbsp; &nbsp; Items = new ObservableCollection<Item>(records);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; MyDataGrid.ItemsSource = Items;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
随时随地看视频慕课网APP
我要回答