有一个ListView在里面写入数据,事实是没有办法一次把所有的东西都写完,所以需要更改/更新已经输入到ListView中的数据。做这个的最好方式是什么?
<ListView x:Name="ClientsInfo">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=ConnectionID}">ID</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=ClientName}">Name</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=Version}">Version</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=ComputerID}">Computer ID</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=ClientIP}">Client IP</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=PingTime}">Ping Time</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
public class ClientsInfo
{
public string ConnectionID { get; set; }
public string ClientName { get; set; }
public string Version { get; set; }
public string ComputerID { get; set; }
public string ClientIP { get; set; }
public string PingTime { get; set; }
}
ClientsInfo.Items.Add(new ClientsInfo
{
ConnectionID = clientNumber.ToString(),
ClientName = "Receiving...",
Version = "Receiving...",
ComputerID = "Receiving...",
ClientIP = clientIP,
PingTime = "..."
});
ClientsInfo.Items.Refresh();
...
ClientsInfo.Items.Add(new ClientsInfo
{
ClientName = clientsName,
Version = versionStr,
ComputerID = computerName,
});
ClientsInfo.Items.Refresh();
...
ClientsInfo.Items.Add(new ClientsInfo
{
PingTime = $"{elapsedTimeInMilliseconds:0.##}ms"
});
ClientsInfo.Items.Refresh();
例子。本质:
窗户:
窗口布局:
从代码中可以看出,我们只使用强类型集合
users
,GUI 中的所有更改都会自动获取。下一步是分离 GUI 和应用程序逻辑。我们以命令的形式描述所有动作,我们将所有数据放在单独的类中(而不是像现在这样,窗口类中的集合)。嗯,这种方法的最高点是 MVVM 模式。