简体   繁体   English

WPF C# - 向DataGrid添加3个字符串

[英]WPF C# - Add 3 string to DataGrid

I'm currently making a Launcher (Imagine something like the LoL launcher) for my Source Engine based game. 我正在为基于Source Engine的游戏制作一个启动器(想象一下像LoL启动器这样的东西)。

I have a working SSQLib that query the server data (Server name, Currentplayers/Maxplayers, Map name). 我有一个工作SSQLib查询服务器数据(服务器名称,当前播放器/ Maxplayers,地图名称)。 I want to list these three data in a DataGrid. 我想在DataGrid中列出这三个数据。 So, how can I list these in a DataGrid? 那么,我如何在DataGrid中列出这些?

Here's my code: 这是我的代码:

private void btnLoadDataGrid_Click(object sender, RoutedEventArgs e)
        {
            string ip = "192.168.1.73";
            int port = 27015;

            IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(ip), port);

            SSQL query = new SSQL();
            ServerInfo serverInformation = query.Server(endpoint);

            string servername = serverInformation.Name.ToString();
            string mapname = serverInformation.Map.ToString();
            string servermaxplayer = serverInformation.MaxPlayers.ToString();
        }

When you use MVVMC and I understand correctly what you try to do, your code should look similar to the following: 当您使用MVVMC并且我正确理解您尝试执行的操作时,您的代码应类似于以下内容:

View: 视图:

<DataGrid x:Name="MyDataGrid"   ItemsSource="{Binding Path=MyObservableCollection}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ServerName" Binding="{Binding servername}"/>
                <DataGridTextColumn Header="MapName" Binding="{Binding mapname}"/>
                <DataGridTextColumn Header="MaxPlayers" Binding="{Binding servermaxplayer}"/>               
            </DataGrid.Columns>
</DataGrid>

ViewModel: 视图模型:

private ObservableCollection<ServerModel> _myObservableCollection;

        public ObservableCollection<ServerModel> MyObservableCollection
        {
            get { return _myObservableCollection; }
            set
            {
                if (_myObservableCollection == value)                                
                    return;
                _myObservableCollection = value;                                     
                OnPropertyChanged("MyObservableCollection");                         
            }
        }

Controller: 控制器:

private LauncherViewModel mViewModel;             

        public void Initialize()                                          
        {
            var view = new Launcher();

            mViewModel = new LauncherViewModel
            {
                MyObservableCollection = new ObservableCollection<ServerModel>(),
            };
            view.DataContext = mViewModel;
            view.ShowDialog();

        }

Model: 模型:

public class ServerModel

public string servername { get; set; }
public string mapname { get; set; }
public int servermaxplayer { get; set; }

Additionally, you should define the servermaxplayer Property as an int, because it is a number, not a string --> see Model. 另外,您应该将servermaxplayer属性定义为int,因为它是一个数字,而不是字符串 - >请参阅Model。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM