数据表格中显示为空白的项目

huangapple go评论50阅读模式
英文:

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&lt;TableData&gt; _tableData = new List&lt;TableData&gt;();
TableData tableData = new TableData
{
    ID = &quot;0&quot;,
    WrestlerID = &quot;000&quot;,
    Name = &quot;test1&quot;
};
_tableData.Add(tableData);
TableData tableData2 = new TableData
{
    ID = &quot;1&quot;,
    WrestlerID = &quot;111&quot;,
    Name = &quot;test2&quot;
};
_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:

&lt;DataGrid x:Name=&quot;DGTable&quot; Grid.Row=&quot;1&quot;&gt;
    &lt;DataGrid.Columns&gt;
        &lt;DataGridTextColumn Header=&quot;ID&quot; Binding=&quot;{Binding ID}&quot; Width=&quot;100&quot;/&gt;
        &lt;DataGridTextColumn Header=&quot;Name&quot; Binding=&quot;{Binding Name}&quot; Width=&quot;*&quot;/&gt;
        &lt;DataGridTextColumn Header=&quot;Wrestler ID&quot; Binding=&quot;{Binding WrestlerID}&quot; Width=&quot;200&quot;/&gt;
    &lt;/DataGrid.Columns&gt;
&lt;/DataGrid&gt;

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 = &quot;&quot;)  
    {  
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }  

    private string id;
    public string ID
    {
        get =&gt; 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.

huangapple
  • 本文由 发表于 2023年6月2日 02:56:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定