DataBinding无法正常工作

时间:2015-06-20 09:08:37

标签: c# wpf datagrid

我制作了一个代码,在反序列化后填充calc_global_nohz,但是人口中存在一些问题。这是在DataGrid中插入数据的代码:

DataGrid

xaml中的每个var leagueTable_Object = JsonConvert.DeserializeObject<LeagueTable.RootObject>(responseText); foreach (var classifica in leagueTable_Object.standing) { League_DataGrid.Items.Add(new LeagueTable.Classifica(){ name = classifica.position + " " + classifica.teamName}); var name = new DataGridTextColumn(); name.Binding = new Binding("name"); League_DataGrid.Columns.Add(name); League_DataGrid.Items.Add(new LeagueTable.Classifica(){ points = classifica.points}); var points = new DataGridTextColumn(); points.Binding = new Binding("points"); League_DataGrid.Columns.Add(points); League_DataGrid.Items.Add(new LeagueTable.Classifica(){ playedGames = classifica.playedGames}); var playedGames = new DataGridTextColumn(); playedGames.Binding = new Binding("playedGames"); League_DataGrid.Columns.Add(playedGames); League_DataGrid.Items.Add(new LeagueTable.Classifica(){ goals = classifica.goals }); var goals = new DataGridTextColumn(); goals.Binding = new Binding("goals"); League_DataGrid.Columns.Add(goals); League_DataGrid.Items.Add(new LeagueTable.Classifica(){ goalsAgainst = classifica.goalsAgainst }); var goalsAgainst = new DataGridTextColumn(); goalsAgainst.Binding = new Binding("goalsAgainst"); League_DataGrid.Columns.Add(goalsAgainst); League_DataGrid.Items.Add(new LeagueTable.Classifica(){ goalsDifference = classifica.goalDifference }); var goalsDifference = new DataGridTextColumn(); goalsDifference.Binding = new Binding("goalsDifference"); League_DataGrid.Columns.Add(goalsDifference); } 都有这样的结构:

DataGrid

这是包含数据结构的类:

 <DataGridTextColumn Binding="{Binding Path = 'name'}" 
          Header="Squadra" 
          CanUserResize="true" MinWidth="150" MaxWidth="200"/>

现在所有数据都插入public struct Classifica { public string name { get; set; } public int points { get; set; } public int position { get; set; } public int playedGames { get; set; } public int goals { get; set; } public int goalsAgainst { get; set; } public int goalsDifference { get; set; } } ,但最终结果如下: enter image description here

如何看待与团队相关的数据放在多行上,这当然不好。此外,数据也包含在Binding中未指定的列中 - 这很奇怪。谁能解释一下我做错了什么?

使用建议解决方案更新图像:

enter image description here

XAML结构:

enter image description here

1 个答案:

答案 0 :(得分:3)

您循环遍历数据集中的每条记录,并将每个列的项目添加到网格中一次。您只需要添加一次列和项目一次。

var leagueTable_Object =
    JsonConvert.DeserializeObject<LeagueTable.RootObject>(responseText);

DataGridTextColumn name = new DataGridTextColumn();
name.Binding = new Binding("name");
League_DataGrid.Columns.Add(name);

DataGridTextColumn points = new DataGridTextColumn();
points.Binding = new Binding("points");
League_DataGrid.Columns.Add(points);

DataGridTextColumn playedGames = new DataGridTextColumn();
playedGames.Binding = new Binding("playedGames");
League_DataGrid.Columns.Add(playedGames);

DataGridTextColumn goals = new DataGridTextColumn();
goals.Binding = new Binding("goals");
League_DataGrid.Columns.Add(goals);

DataGridTextColumn goalsAgainst = new DataGridTextColumn();
goalsAgainst.Binding = new Binding("goalsAgainst");
League_DataGrid.Columns.Add(goalsAgainst);

DataGridTextColumn goalsDifference = new DataGridTextColumn();
goalsDifference.Binding = new Binding("goalsDifference");
League_DataGrid.Columns.Add(goalsDifference);

foreach (var classifica in leagueTable_Object.standing)
{
    League_DataGrid.Items.Add(new LeagueTable.Classifica
    {
        name = classifica.position + " " + classifica.teamName,
        points = classifica.points,
        playedGames = classifica.playedGames,
        goals = classifica.goals,
        goalsAgainst = classifica.goalsAgainst,
        goalsDifference = classifica.goalDifference
    });

}

另请注意,这可能不是绑定数据的最佳方式。例如,最好在Xaml代码中将列添加到网格中。

修改

在您的修改显示您的Xaml代码后,现在很清楚您不需要手动添加列,因此您只需要foreach内的代码。此外,一些Linq可以进一步简化:

var leagueTable_Object = 
    JsonConvert.DeserializeObject<LeagueTable.RootObject>(responseText);

League_DataGrid.Items.AddRange(
    leagueTable_Object.standing.Select(c => new
    {
        name = c.position + " " + c.teamName,
        points = c.points,
        playedGames = c.playedGames,
        goals = c.goals,
        goalsAgainst = c.goalsAgainst,
        goalsDifference = c.goalDifference
    }));