如何在C#WPF DataGrid中获取选定的行值?

时间:2017-03-25 02:15:42

标签: c# wpf powershell datagrid

我使用Powershell命令在C#WPF中编写它。

我首先创建Datagrid列对象:

public class Item
{
    public object ID { get; set; }
    public object ProcessName { get; set; }
    public object Handles { get; set; }
    public object NPM { get; set; }
    public object PM { get; set; }
    public object WS { get; set; }
    public object Path { get; set; }
}

然后,下面的代码填充了一个显示PC上所有正在运行的进程的Datagrid:

private void btnGetProcess_Click(object sender, RoutedEventArgs e)
{
    //Creates fields
    DataGridTextColumn c1 = new DataGridTextColumn();
    c1.Header = "ID";
    c1.Binding = new Binding("ID");

    dgdProcessList.Columns.Add(c1);
    DataGridTextColumn c2 = new DataGridTextColumn();
    c2.Header = "Process Name";
    c2.Binding = new Binding("ProcessName");
    dgdProcessList.Columns.Add(c2);

    DataGridTextColumn c3 = new DataGridTextColumn();
    c3.Header = "Handles";
    c3.Binding = new Binding("Handles");
    dgdProcessList.Columns.Add(c3);

    DataGridTextColumn c4 = new DataGridTextColumn();
    c4.Header = "NPM(K)";
    c4.Binding = new Binding("NPM");
    dgdProcessList.Columns.Add(c4);

    DataGridTextColumn c5 = new DataGridTextColumn();
    c5.Header = "PM(K)";
    c5.Binding = new Binding("PM");
    dgdProcessList.Columns.Add(c5);

    DataGridTextColumn c6 = new DataGridTextColumn();
    c6.Header = "WS(K)";
    c6.Binding = new Binding("WS");
    dgdProcessList.Columns.Add(c6);

    DataGridTextColumn c7 = new DataGridTextColumn();
    c6.Header = "Path";
    c6.Binding = new Binding("Path");
    dgdProcessList.Columns.Add(c7);

    //Connection to hostname successful
    Runspace runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();

    PowerShell ps = PowerShell.Create(); // Create a new PowerShell instance
    ps.Runspace = runspace; // Add the instance to the runspace
    ps.Commands.AddScript("Get-Process | Select-Object ID, ProcessName, Handles, NPM, PM, WS, Path"); // Add a script
    Collection<PSObject> results = ps.Invoke();

    runspace.Close();

    foreach (PSObject obj in results)
    {
        object IDOutput;
        object ProcessNameOutput;
        object HandlesOutput;
        object NPMOutput;
        object PMOutput;
        object WSOutput;
        object PathOutput;

        IDOutput = obj.Properties["ID"].Value;
        ProcessNameOutput = obj.Properties["ProcessName"].Value;
        HandlesOutput = obj.Properties["Handles"].Value;
        NPMOutput = obj.Properties["NPM"].Value;
        PMOutput = obj.Properties["PM"].Value;
        WSOutput = obj.Properties["WS"].Value;
        PathOutput = obj.Properties["Path"].Value;
        dgdProcessList.Items.Add(new Item() { ID = IDOutput, ProcessName = ProcessNameOutput, Handles = HandlesOutput, NPM = NPMOutput, PM = PMOutput, WS = WSOutput, Path = PathOutput });
    }
}

我试图找出如何获取所选行值。更具体地说,我希望能够获得&#34; ID&#34;我选择的行的字段值。

我花了最后几个小时玩不同的属性变体(SelectedItem,SelectedValue,GetValue等),但我似乎无法让它工作。例如,此代码返回一个值(ProjectName.FormName + Item),但不返回实际的对象值:

MessageBox.Show(dgdProcessList.SelectedValue.ToString());

任何帮助表示赞赏!谢谢。

1 个答案:

答案 0 :(得分:0)

SelectedItem的{​​{1}}属性投射到您的DataGrid类型:

Item