用户列表不必存储字符串。它可以存储项目,您可以选择显示它存储的项目的用户名。
您的项目是在 WPF 还是 Winform 中?
否则,要从屏幕名称中获取用户,您可以执行以下操作:
var user = User.GetUserFromScreenName("USER_SCREEN_NAME")
WPF 示例:MainWindow.xaml.cs
var u1 = User.GetUserFromScreenName("hi");
var u2 = User.GetUserFromScreenName("hi2");
UsersListBox.Items.Add(u1);
UsersListBox.Items.Add(u2);
UsersListBox.SelectionChanged += (sender, args) =>
{
SelectedUser = (User)UsersListBox.SelectedItems[0];
// Code logic...
};
主窗口.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox x:Name="UsersListBox" DisplayMemberPath="Name" SelectionMode="Single" />
</Grid>
</Window>