[C#] WPF - 如何填充数据网格并以编程方式编辑项目。 [英] [C#] WPF - How to populate a datagrid and edit the items programmatically.
问题描述
这是一些Launcher.cs类
private void ServerListenerName( int id, string name)
{
Console.WriteLine( 服务器{0}名称= {1},id,name);
Application.Current.Dispatcher.Invoke((Action)(()= >
{
w.addName(id, name);
}));
}
private void ServerListenerAttr(< span class =code-keyword> int id, string name, string value )
{
Console.WriteLine( 服务器{0} attr {1} = {2},id,name, value );
}
private void ServerListenerCap( int id, int cap0, int cap1)
{
Console.WriteLine( server {0} cap0 = {1} cap1 = {2},id,cap0,cap1);
}
private void ServerListenerState( int id, int state)
{
Console.WriteLine( server {0} state = {1},id,state);
}
private void ServerListenerPlayers(< span class =code-keyword> int id, int players)
{
Console.WriteLine( server {0} players = {1},id,players);
}
private void ServerListenerAddr( int id, string ip, int port)
{
Console.WriteLine( server {0} ip = {1} port = {2},id,ip,port);
}
如你所见,它分为不同的事件。我希望将每个事件的特定部分放到一个字符串中传递给窗口,窗口是w。如果你看,他们都有(int id)
,那么有没有办法根据他们的id为我的datagrid的项添加信息?这是w.addName(id,name)的代码;
public void addName(int sID,string sName)
{
servl.Items。添加(新zList {zID = sID,zName = sName,zMap =map,zPlayers =10/64,zGamemode =tdm,zPing = 74});
}
公共类zList
{
public int zID {get;组; }
public string zName {get;组; }
public string zMap {get;组; }
public string zPlayers {get;组; }
public string zGamemode {get;组; }
public int zPing {get;组; }
}
不要介意 servl.Items.Add(new zList {zID =下的临时字符串/整数sID,zName = sName,zMap =map,zPlayers =10/64,zGamemode =tdm,zPing = 74});
,因为它们仅用于一般列表填充。
数据网格的XAML
< DataGrid x:名称 = servl HorizontalAlignment = 左 保证金 = 175,10, 0,0 VerticalAlignment = Top 高度 = 676 宽度 = 1054 FontFamily = Geared Slab >
< DataGrid.Columns >
< DataGridTextColumn Binding = {Bin ding Path = zID} ClipboardContentBinding = {x:Null} 前景 = #FFDADADA FontWeight = 粗体 FontFamily = Geared Slab 标题 = ID SortDirection = 升序 CanUserResize = False 宽度 = 40 / >
< DataGridTextColumn 绑定 = {Binding Path = zName} ClipboardContentBinding = {x:Null} 前景 < span class =code-keyword> = #FF DADADA 标题 = 名称 CanUserResize = False FontFamily = Geared Slab 宽度 = 250 / >
< DataGridTextColumn 绑定 = {Binding Path = zMap} ClipboardContentBinding = {x:Null} 前景 < span class =code-keyword> = #FFDADADA 标题 = 地图 CanUserResize = False 宽度 = < span class =code-keyword> 130 / >
< DataGridTextColumn Binding = {x:Null} ClipboardContentBinding = {x:Null} 前景 = #FFDADADA 标题 = 玩家 CanUserResize = False 宽度 = 85 / >
< DataGridTextColumn Binding = < span class =code-keyword> {x:Null} ClipboardContentBinding = {x:Null} 前景 = #FFDADADA 标题 = Gamemode CanUserResize = False 宽度 = 135 / >
< ; DataGridTextColumn 绑定 < span class =code-keyword> = {x:Null} < span class =code-attribute> ClipboardContentBinding = {x:Null} 前景 = #FFDADADA 标题 = Ping / >
< /DataGrid.Columns >
< / DataGrid >
所以对于TL; DR [太长了,没读过],我想将服务器添加到我的数据网格中,但考虑到Launcher.cs代码的设置,我不知道如何。感谢您的时间。
您需要将ItemsSource绑定到zList
Hey there. I've been struggling with developing for a few days now. Caught in a loop, because I'm always tired due to a busy schedule, and not concentrated, so thinking has been difficult. But I have a datagrid I want to programmatically populate. The program signs into a server, and gets a list of game servers. I want to populate my datagrid with information such as ping, and maps, etc, and what gets me is how the code is set up. I wouldn't know how to add all of the information needed for a row. My wording is also pretty terrible, due to lack of rest. Haha. Anywho here's some examples.
This is some of the Launcher.cs Class
private void ServerListenerName(int id, string name)
{
Console.WriteLine("server {0} name = {1}", id, name);
Application.Current.Dispatcher.Invoke((Action)(() =>
{
w.addName(id, name);
}));
}
private void ServerListenerAttr(int id, string name, string value)
{
Console.WriteLine("server {0} attr {1} = {2}", id, name, value);
}
private void ServerListenerCap(int id, int cap0, int cap1)
{
Console.WriteLine("server {0} cap0 = {1} cap1 = {2}", id, cap0, cap1);
}
private void ServerListenerState(int id, int state)
{
Console.WriteLine("server {0} state = {1}", id, state);
}
private void ServerListenerPlayers(int id, int players)
{
Console.WriteLine("server {0} players = {1}", id, players);
}
private void ServerListenerAddr(int id, string ip, int port)
{
Console.WriteLine("server {0} ip = {1} port = {2}", id, ip, port);
}
As you can see, it's broken up into different events. I was hoping to get specific parts of each event into a single string to pass to the window, window being the "w". If you look, all of them have the (int id)
, so would there be a way to add information to my datagrid's items based on their id? Here's the code for w.addName(id, name);
public void addName(int sID, string sName) { servl.Items.Add(new zList { zID = sID, zName = sName, zMap = "map", zPlayers = "10/64", zGamemode = "tdm", zPing = 74}); } public class zList { public int zID { get; set; } public string zName { get; set; } public string zMap { get; set; } public string zPlayers { get; set; } public string zGamemode { get; set; } public int zPing { get; set; } }
Don't mind the temporary strings/integers under the "servl.Items.Add(new zList { zID = sID, zName = sName, zMap = "map", zPlayers = "10/64", zGamemode = "tdm", zPing = 74});
", as they are just for general list population.
XAML for the Datagrid
<DataGrid x:Name="servl" HorizontalAlignment="Left" Margin="175,10,0,0" VerticalAlignment="Top" Height="676" Width="1054" FontFamily="Geared Slab">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=zID}" ClipboardContentBinding="{x:Null}" Foreground="#FFDADADA" FontWeight="Bold" FontFamily="Geared Slab" Header="ID" SortDirection="Ascending" CanUserResize="False" Width="40"/>
<DataGridTextColumn Binding="{Binding Path=zName}" ClipboardContentBinding="{x:Null}" Foreground="#FFDADADA" Header="Name" CanUserResize="False" FontFamily="Geared Slab" Width="250"/>
<DataGridTextColumn Binding="{Binding Path=zMap}" ClipboardContentBinding="{x:Null}" Foreground="#FFDADADA" Header="Map" CanUserResize="False" Width="130"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Foreground="#FFDADADA" Header="Players" CanUserResize="False" Width="85"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Foreground="#FFDADADA" Header="Gamemode" CanUserResize="False" Width="135"/>
<DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Foreground="#FFDADADA" Header="Ping"/>
</DataGrid.Columns>
</DataGrid>
So for a TL;DR [Too long, didn't read], I want to add servers to my datagrid, but considering the set up of Launcher.cs's code, I wouldn't know how. Thanks for your time.
You need to bind the ItemsSource to the zList
这篇关于[C#] WPF - 如何填充数据网格并以编程方式编辑项目。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!