英文:
Items are showing blank in DataGrid
问题
我有一个TableData
类:
public class TableData
{
public string ID, WrestlerID;
public string Name;
}
然后我将一些数据放在列表中:
List<TableData> _tableData = new List<TableData>();
TableData tableData = new TableData
{
ID = "0",
WrestlerID = "000",
Name = "test1"
};
_tableData.Add(tableData);
TableData tableData2 = new TableData
{
ID = "1",
WrestlerID = "111",
Name = "test2"
};
_tableData.Add(tableData2);
然后,我遍历我的_tableData
列表,并将每个项目添加到我的DataGrid:
foreach (TableData data1 in _tableData)
{
DGTable.Items.Add(data1);
}
顺便说一下,这是我的DataGrid:
<DataGrid x:Name="DGTable" Grid.Row="1">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" Width="100"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*"/>
<DataGridTextColumn Header="Wrestler ID" Binding="{Binding WrestlerID}" Width="200"/>
</DataGrid.Columns>
</DataGrid>
当我运行应用程序时,DataGrid 显示了 2 行,但所有字段都是空的。有什么想法吗?谢谢!
英文:
I have a TableData
class:
public class TableData
{
public string ID, WrestlerID;
public string Name;
}
And some data that I then put on a list:
List<TableData> _tableData = new List<TableData>();
TableData tableData = new TableData
{
ID = "0",
WrestlerID = "000",
Name = "test1"
};
_tableData.Add(tableData);
TableData tableData2 = new TableData
{
ID = "1",
WrestlerID = "111",
Name = "test2"
};
_tableData.Add(tableData2);
I then iterate through my _tableData
list and add each item on my DataGrid:
foreach (TableData data1 in _tableData)
{
DGTable.Items.Add(data1);
}
BTW Here's my DataGrid:
<DataGrid x:Name="DGTable" Grid.Row="1">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" Width="100"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*"/>
<DataGridTextColumn Header="Wrestler ID" Binding="{Binding WrestlerID}" Width="200"/>
</DataGrid.Columns>
</DataGrid>
When I run the app, the DataGrid displays 2 rows but all fields are empty. Any thoughts? Thanks!
答案1
得分: 0
您的 TableData
类需要使用属性而不是字段,以便能够使用数据绑定。
它还应该实现 INotifyPropertyChanged 接口以使用可观察属性,以便对这些属性的更改在 UI 中得到反映。
请按照以下方式更改您的类:
using System.ComponentModel;
public class TableData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string id;
public string ID
{
get => id;
set
{
if (id == value) return;
id = value;
OnPropertyChanged();
}
}
// 重复上述步骤以处理 WrestlerID 和 Name
//...
}
不要忘记在顶部添加 using System.ComponentModel;
。
英文:
Your TableData
class needs to have properties instead of fields to be able use bindings.
It should also implement the INotifyPropertyChanged interface to use observable properties, so that changes to those properties get reflected in the UI.
Change your class as follows:
public class TableData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string id;
public string ID
{
get => id;
set
{
if(id == value) return;
id = value;
OnPropertyChanged();
}
}
// repeat for WrestlerID and Name
//...
}
Don't forget to add using System.ComponentModel;
at the top.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论