我使用dotnetrdf,我想在 WPF 中显示查询结果。这是我在 ViewModel 中的功能。我有我认为接下来要使用的 DataTable。
//Results
SparqlResultSet results;
DataTable table;
//Define a remote endpoint
//Use the DBPedia SPARQL endpoint with the default Graph set to DBPedia
SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org");
//Make a SELECT query against the Endpoint
results = endpoint.QueryWithResultSet("PREFIX dbo: <http://dbpedia.org/ontology/> PREFIX : <http://dbpedia.org/resource/> SELECT ?film ?producerName WHERE { ?film dbo:director :Andrzej_Wajda . ?film dbo:producer ?producerName . }");
foreach (SparqlResult result in results)
{
Console.WriteLine(result.ToString());
}
table = new DataTable();
DataRow row;
switch (results.ResultsType)
{
case SparqlResultsType.VariableBindings:
foreach (String var in results.Variables)
{
table.Columns.Add(new DataColumn(var, typeof(INode)));
}
foreach (SparqlResult r in results)
{
row = table.NewRow();
foreach (String var in results.Variables)
{
if (r.HasValue(var))
{
row[var] = r[var];
}
else
{
row[var] = null;
}
}
table.Rows.Add(row);
}
break;
case SparqlResultsType.Boolean:
table.Columns.Add(new DataColumn("ASK", typeof(bool)));
row = table.NewRow();
row["ASK"] = results.Result;
table.Rows.Add(row);
break;
case SparqlResultsType.Unknown:
default:
throw new InvalidCastException("Unable to cast a SparqlResultSet to a DataTable as the ResultSet has yet to be filled with data and so has no SparqlResultsType which determines how it is cast to a DataTable");
}
在 WPF 中,我使用代码:
<DataGrid ItemsSource="{Binding Table}" AutoGenerateColumns="True"/>
绑定工作得很好,最后我得到了动态创建的列和 DataGrid,但只有标题。我没有得到行的价值。在此示例中,有行,但没有值。
我的问题在哪里?非常感谢您的帮助:)