C# UWP社区工具包DataGrid未显示数据
我试图在UWP中加载一个数据网格(来自社区工具包包),其中包含来自ObservableCollection的数据。将显示从中读取数据的CSV文件的标题,但由于某些原因,没有显示任何数据行。我已经通读并尝试了至少5到6个问题,所以虽然我知道以前有人问过这个问题,但这些答案似乎不起作用 在这一点上,我完全不知道自己做错了什么。我知道我的代码中有一些错误(很明显,否则它会起作用),但我似乎无法找到它。有人能看一下下面的代码,看看他们是否能发现任何错误吗 注意:我知道数据被正确读取,因为a)标题显示,b)我点击了断点并查看了SpellBook中的数据,其中包含408项 提前感谢任何能提供帮助的人 C#C# UWP社区工具包DataGrid未显示数据,c#,uwp,datagrid,observablecollection,windows-community-toolkit,C#,Uwp,Datagrid,Observablecollection,Windows Community Toolkit,我试图在UWP中加载一个数据网格(来自社区工具包包),其中包含来自ObservableCollection的数据。将显示从中读取数据的CSV文件的标题,但由于某些原因,没有显示任何数据行。我已经通读并尝试了至少5到6个问题,所以虽然我知道以前有人问过这个问题,但这些答案似乎不起作用 在这一点上,我完全不知道自己做错了什么。我知道我的代码中有一些错误(很明显,否则它会起作用),但我似乎无法找到它。有人能看一下下面的代码,看看他们是否能发现任何错误吗 注意:我知道数据被正确读取,因为a)标题显示,b
公共密封部分类SpellPageBase:第页
{
公共ObservableCollection拼写本{get;set;}
公共列表拼写数据{get;set;}
公共静态只读从属属性组件属性=
DependencyProperty.Register(“组件”、typeof(字符串)、typeof(SpellPageBase)、new PropertyMetadata(“组件”);
公共静态只读DependencyProperty DescriptionProperty=
DependencyProperty.Register(“说明”)、typeof(字符串)、typeof(SpellPageBase)、new PropertyMetadata(“说明”);
公共静态只读从属属性HighChangesProperty=
DependencyProperty.Register(“HighChanges”、typeof(string)、typeof(SpellPageBase)、new PropertyMetadata(“HighLevelChanges”);
公共静态只读DependencyProperty HighDescriptionProperty=
DependencyProperty.Register(“HighDescription”、typeof(string)、typeof(SpellPageBase)、new PropertyMetadata(“HighLevelDescription”);
公共静态只读从属属性SchoolLevelProperty=
DependencyProperty.Register(“SpellLevel”、typeof(int)、typeof(SpellPageBase)、new PropertyMetadata(0));
公共静态只读从属属性IsRitualProperty=
DependencyProperty.Register(“IsRitual”、typeof(bool)、typeof(SpellPageBase)、new PropertyMetadata(false));
公共静态只读从属属性SchoolProperty=
DependencyProperty.Register(“学校”)、typeof(字符串)、typeof(SpellPageBase)、new PropertyMetadata(“学校”);
公共静态只读DependencyProperty SpellNameProperty=
DependencyProperty.Register(“SpellName”、typeof(string)、typeof(SpellPageBase)、new PropertyMetadata(“Name”);
公共字符串组件
{
get{return(string)GetValue(ComponentProperty);}
set{SetValue(componentsprroperty,value);}
}
公共字符串描述
{
get{return(string)GetValue(DescriptionProperty);}
set{SetValue(DescriptionProperty,value);}
}
公共字符串更改
{
获取{return(string)GetValue(HighChangesProperty);}
set{SetValue(HighChangesProperty,value);}
}
公共字符串高描述
{
get{return(string)GetValue(HighDescriptionProperty);}
集合{SetValue(HighDescriptionProperty,value);}
}
公共图书馆
{
获取{return(bool)GetValue(IsRitualProperty);}
set{SetValue(IsRitualProperty,value);}
}
公立弦乐学校
{
获取{return(string)GetValue(SchoolProperty);}
set{SetValue(学校属性,值);}
}
公立学校
{
获取{return(int)GetValue(SchoolLevelProperty);}
set{SetValue(SchoolLevelProperty,value);}
}
公共字符串拼写名称
{
获取{return(string)GetValue(SpellNameProperty);}
set{SetValue(SpellNameProperty,value);}
}
公共拼写页面库()
{
SpellBook=新的ObservableCollection();
this.InitializeComponent();
DataContext=this;
//加载();
//ReadFile();
}
公共void ReadFile()
{
使用(CsvReader csv=new CsvReader(new StreamReader(“Assets\\Spells5thEdition.csv”),true))
{
int fieldCount=csv.fieldCount;
string[]headers=csv.GetFieldHeaders();
而(csv.ReadNextRecord())
{
字符串键=csv[0];
字符串名称=csv[1];
bool isRitual=csv[2]=“1”?真:假;
字符串ritualName=csv[3];
int-level=int.Parse(csv[4]);
TryParse(csv[5],校外);
//int学校=新学校(int.Parse(csv[5]);
字符串schoolevel=csv[6];
字符串castingTime=csv[7];
字符串范围=csv[8];
字符串持续时间=csv[9];
字符串组件=csv[10];
字符串材料=csv[11];
字符串描述=csv[12];
字符串higherLevelMods=csv[13];
字符串higherLevelDescription=csv[14];
字符串save=csv[15];
字符串类=csv[16];
字符串sourceBook=csv[17];
字符串页=csv[18];
字符串bookPage=csv[19];
拼写数据。添加(新拼写(关键字、名称、isRitual、仪式名称、级别、学校、学校级别、施放时间、范围、持续时间、组件、材料、说明、高级MODS、高级说明、保存、类、源代码簿、页面、书页));
}
}
ObservableCollection拼写书=新的ObservableCollection(拼写数据);
}
已加载私有无效页面(对象发送方,路由目标e)
{
this.spellData=新列表();
ReadFile();
//foreach(拼写数据中的拼写s)
//{
//拼写书。添加(s);
//}
this.spellsList.DataContext=this.SpellBook;
public sealed partial class SpellPageBase : Page
{
public ObservableCollection<Spell> SpellBook { get; set; }
public List<Spell> spellData { get; set; }
public static readonly DependencyProperty ComponentsProperty =
DependencyProperty.Register("Components", typeof(string), typeof(SpellPageBase), new PropertyMetadata("Components"));
public static readonly DependencyProperty DescriptionProperty =
DependencyProperty.Register("Description", typeof(string), typeof(SpellPageBase), new PropertyMetadata("Description"));
public static readonly DependencyProperty HighChangesProperty =
DependencyProperty.Register("HighChanges", typeof(string), typeof(SpellPageBase), new PropertyMetadata("HighLevelChanges"));
public static readonly DependencyProperty HighDescriptionProperty =
DependencyProperty.Register("HighDescription", typeof(string), typeof(SpellPageBase), new PropertyMetadata("HighLevelDescription"));
public static readonly DependencyProperty SchoolLevelProperty =
DependencyProperty.Register("SpellLevel", typeof(int), typeof(SpellPageBase), new PropertyMetadata(0));
public static readonly DependencyProperty IsRitualProperty =
DependencyProperty.Register("IsRitual", typeof(bool), typeof(SpellPageBase), new PropertyMetadata(false));
public static readonly DependencyProperty SchoolProperty =
DependencyProperty.Register("School", typeof(string), typeof(SpellPageBase), new PropertyMetadata("School"));
public static readonly DependencyProperty SpellNameProperty =
DependencyProperty.Register("SpellName", typeof(string), typeof(SpellPageBase), new PropertyMetadata("Name"));
public string Components
{
get { return (string)GetValue(ComponentsProperty); }
set { SetValue(ComponentsProperty, value); }
}
public string Description
{
get { return (string)GetValue(DescriptionProperty); }
set { SetValue(DescriptionProperty, value); }
}
public string HighChanges
{
get { return (string)GetValue(HighChangesProperty); }
set { SetValue(HighChangesProperty, value); }
}
public string HighDescription
{
get { return (string)GetValue(HighDescriptionProperty); }
set { SetValue(HighDescriptionProperty, value); }
}
public bool IsRitual
{
get { return (bool)GetValue(IsRitualProperty); }
set { SetValue(IsRitualProperty, value); }
}
public string School
{
get { return (string)GetValue(SchoolProperty); }
set { SetValue(SchoolProperty, value); }
}
public int SchoolLevel
{
get { return (int)GetValue(SchoolLevelProperty); }
set { SetValue(SchoolLevelProperty, value); }
}
public string SpellName
{
get { return (string)GetValue(SpellNameProperty); }
set { SetValue(SpellNameProperty, value); }
}
public SpellPageBase()
{
SpellBook = new ObservableCollection<Spell>();
this.InitializeComponent();
DataContext = this;
//Load();
//ReadFile();
}
public void ReadFile()
{
using (CsvReader csv = new CsvReader(new StreamReader("Assets\\Spells5thEdition.csv"), true))
{
int fieldCount = csv.FieldCount;
string[] headers = csv.GetFieldHeaders();
while(csv.ReadNextRecord())
{
string key = csv[0];
string name = csv[1];
bool isRitual = csv[2] == "1" ? true : false;
string ritualName = csv[3];
int level = int.Parse(csv[4]);
Enum.TryParse(csv[5], out School school);
//int school = new School(int.Parse(csv[5]));
string schoolLevel = csv[6];
string castingTime = csv[7];
string range = csv[8];
string duration = csv[9];
string components = csv[10];
string materials = csv[11];
string description = csv[12];
string higherLevelMods = csv[13];
string higherLevelDescription = csv[14];
string save = csv[15];
string classes = csv[16];
string sourceBook = csv[17];
string page = csv[18];
string bookPage = csv[19];
spellData.Add(new Spell(key, name, isRitual, ritualName, level, school, schoolLevel, castingTime, range, duration, components, materials, description, higherLevelMods, higherLevelDescription, save, classes, sourceBook, page, bookPage));
}
}
ObservableCollection<Spell> spellBook = new ObservableCollection<Spell>(spellData);
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
this.spellData = new List<Spell>();
ReadFile();
//foreach(Spell s in spellData)
//{
// SpellBook.Add(s);
//}
this.spellsList.DataContext = this.SpellBook;
spellsList.ItemsSource = SpellBook;
spellsList.UpdateLayout();
}
<Page
x:Class="DungeoneerKit.SpellPageBase"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DungeoneerKit"
xmlns:tk="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Loaded="Page_Loaded"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.Resources>
<DataTemplate x:Key="RowDetailsTemplate">
<StackPanel>
<TextBlock Text="Spell Details:" Margin="10" />
<Grid Margin="20, 10" Padding="5">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Description: " FontWeight="SemiBold" FontSize="13" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="At High-Levels: " FontWeight="SemiBold" FontSize="13" />
<TextBlock Grid.Row="2" Grid.Column="0" Text="High-Level Description: " FontWeight="SemiBold" FontSize="13" />
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Description}" FontSize="13" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding HighDescription}" FontSize="13" />
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding HighChanges}" FontSize="13" />
</Grid>
</StackPanel>
</DataTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<tk:DataGrid x:Name="spellsList"
Height="900"
Foreground="White"
DataContext="{Binding SpellBook}"
ItemsSource="{Binding SpellBook}"
Grid.Row="1"
Margin="12"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible"
AlternatingRowBackground="Transparent"
AlternatingRowForeground="Gray"
AreRowDetailsFrozen="False"
AutoGenerateColumns="True"
CanUserSortColumns="True"
CanUserResizeColumns="True"
CanUserReorderColumns="True"
ColumnHeaderHeight="32"
MaxColumnWidth="400"
IsReadOnly="False"
RowDetailsTemplate="{StaticResource RowDetailsTemplate}"
RowDetailsVisibilityMode="VisibleWhenSelected"
SelectionMode="Extended">
<!--<tk:DataGrid.Columns>
<tk:DataGridTextColumn Header="Spell Name" Binding="{Binding SpellName}" />
<tk:DataGridCheckBoxColumn Header="Ritual" Binding="{Binding IsRitual}" />
<tk:DataGridTextColumn Header="Level-School" Binding="{Binding SchoolLevel}" />
<tk:DataGridTextColumn Header="Components" Binding="{Binding Components}" />
</tk:DataGrid.Columns>-->
</tk:DataGrid>
</Grid>
<controls:DataGrid
x:Name="MyDataGrid"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AlternatingRowBackground="Transparent"
AlternatingRowForeground="Gray"
AreRowDetailsFrozen="False"
AreRowGroupHeadersFrozen="True"
AutoGenerateColumns="False"
CanUserReorderColumns="True"
CanUserResizeColumns="True"
CanUserSortColumns="False"
ColumnHeaderHeight="32"
FrozenColumnCount="0"
GridLinesVisibility="None"
HeadersVisibility="Column"
HorizontalScrollBarVisibility="Visible"
IsReadOnly="False"
Loaded="DataGrid_Loaded"
MaxColumnWidth="400"
RowDetailsVisibilityMode="Collapsed"
RowGroupHeaderPropertyNameAlternative="Range"
SelectionMode="Extended"
VerticalScrollBarVisibility="Visible"
>
<controls:DataGrid.RowGroupHeaderStyles>
<Style TargetType="controls:DataGridRowGroupHeader">
<Setter Property="Background" Value="LightGray" />
</Style>
</controls:DataGrid.RowGroupHeaderStyles>
<controls:DataGrid.Columns>
<controls:DataGridTextColumn
Binding="{Binding Id}"
Header="Id"
Tag="Id"
/>
<controls:DataGridTextColumn
Binding="{Binding Title}"
Header="Title"
Tag="Title"
/>
<controls:DataGridComboBoxColumn
Binding="{Binding Link}"
Header="Link"
Tag="Link"
/>
<controls:DataGridTextColumn
Binding="{Binding Type}"
Header="Type"
Tag="Type"
/>
<controls:DataGridTextColumn
Binding="{Binding Remark}"
Header="Remark"
Tag="Remark"
/>
<controls:DataGridTextColumn
Binding="{Binding Time}"
Header="Time"
Tag="Time"
/>
</controls:DataGrid.Columns>
</controls:DataGrid>
public class Item
{
public string Id { get; set; }
public string Title { get; set; }
public string Link { get; set; }
public string Type { get; set; }
public string Remark { get; set; }
public string Time { get; set; }
}
private ObservableCollection<Item> Items;
private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
using (var reader = new StreamReader("Assets\\Archive.csv",true))
using (var csv = new CsvReader(reader))
{
var records = csv.GetRecords<Item>();
Items = new ObservableCollection<Item>(records);
}
MyDataGrid.ItemsSource = Items;
}