我一直在尝试加载&将JSON文件验证为数据杂交几天(使用JSON.NET),并且网格不断变为空白。我敢肯定,这对大多数人来说都是显而易见的,我可能会缺少一些主要项目...但是我正在学习,似乎无法弄清楚这一点。感谢一些指导!
这是我的XAML:
<DataGrid x:Name="dataGrid"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AutoGenerateColumns="False"
ItemsSource="{Binding items}" Margin="0,36,0,0">
<DataGrid.Columns>
<DataGridTextColumn Header="projectNumber" Binding="{Binding Path=projectNumber}"/>
<DataGridTextColumn Header="projectName" Binding="{Binding Path=projectName}"/>
<DataGridTextColumn Header="Directory1" Binding="{Binding Path=Directory1}"/>
<DataGridTextColumn Header="Directory2" Binding="{Binding Path=Directory2}"/>
<DataGridTextColumn Header="Directory3" Binding="{Binding Path=Directory3}"/>
</DataGrid.Columns>
</DataGrid>
这是我的后端:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"C:ProjectsJSONimportprojectList.json"))
{
JsonSerializer serializer = new JsonSerializer();
ProjectThread projectThread2 = (ProjectThread)serializer.Deserialize(file, typeof(ProjectThread));
}
}
public void LoadJson()
{
using (StreamReader r = new StreamReader(@"C:ProjectsJSONimportprojectList.json"))
{
string json = r.ReadToEnd();
ObservableCollection<ProjectThread> items = JsonConvert.DeserializeObject<ObservableCollection<ProjectThread>>(json);
}
}
}
public class ProjectThread
{
public String projectNumber { get; set; }
public String projectName { get; set; }
public String Directory1 { get; set; }
public String Directory2 { get; set; }
public String Directory3 { get; set; }
}
这是我的测试器JSON文件格式:
{
"projectNumber": "100",
"projectName": "sample",
"Directory1": "x",
"Directory2": "x",
"Directory3": "x"
},
{
"projectNumber": "101",
"projectName": "sample 2",
"Directory1": "x",
"Directory2": "x",
"Directory3": "x"
},
我以以下方式更改了您的代码,它对我有用。我认为您将私有字段绑定到数据杂志。您正在loadJson函数中初始化项目的新对象。
ViewModel
private ObservableCollection<ProjectThread> _items;
public ObservableCollection<ProjectThread> Items
{
get { return _items; }
set
{
_items = value;
this.NotifyPropertyChanged("Items");
}
}
public ViewModel()
{
Items = new ObservableCollection<ProjectThread>();
this.LoadJson();
}
public void LoadJson()
{
using (StreamReader r = new StreamReader(@"d:file.txt"))
{
string json = r.ReadToEnd();
Items = JsonConvert.DeserializeObject<ObservableCollection<ProjectThread>>(json);
}
}
xaml
<DataGrid x:Name="dataGrid"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AutoGenerateColumns="False"
ItemsSource="{Binding Items}" Margin="0,36,0,0">
<DataGrid.Columns>
<DataGridTextColumn Header="projectNumber" Binding="{Binding Path=projectNumber}"/>
<DataGridTextColumn Header="projectName" Binding="{Binding Path=projectName}"/>
<DataGridTextColumn Header="Directory1" Binding="{Binding Path=Directory1}"/>
<DataGridTextColumn Header="Directory2" Binding="{Binding Path=Directory2}"/>
<DataGridTextColumn Header="Directory3" Binding="{Binding Path=Directory3}"/>
</DataGrid.Columns>
</DataGrid>
json
[{
"projectNumber": "100",
"projectName": "sample",
"Directory1": "x",
"Directory2": "x",
"Directory3": "x"
},
{
"projectNumber": "101",
"projectName": "sample 2",
"Directory1": "x",
"Directory2": "x",
"Directory3": "x"
}]
伙计们,您的输入都奏效和帮助。贾斯汀(Justin)也是我对MVVM的介绍,因此感谢您像ViewModel类中指定您的代码一样。当它适用于您正在处理的事情时,总是更容易学习!!!