简体   繁体   中英

How to dynamically onload a list of running processes on a WPF Application Combobox/ ListView?

I'm currently creating an WPF application that once loaded, provides a list of running processes running on the local system for users to select and then do something with the selected process.

However i just cant get the list of processes to be linked and shown in the list view after my coding.

I have tried:

  • Adding the Loaded="Window_Loaded" attribute to the start of the XAML file
  • creating the myProcess class in my class library and linking it as reference to my Main application file

C#

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            Process[] processes = Process.GetProcesses();
            List<myProcess> myProcess = new List<myProcess>();
            foreach (Process item in processes)
            {
                myProcess.Add(new myProcess() { processName = item.ProcessName, processId = item.Id });
            }
        }
    }

myProcess Class

namespace TraceLibrary
{
    public class myProcess
    {
        public string processName { get; set; }
        public int processId { get; set; }
    }
}

XAML

<ListView Grid.Column="4" Grid.Row="4" Margin="230,0,0,0" Grid.ColumnSpan="2" Name="listViewProcess">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" Width="200" DisplayMemberBinding="{Binding processName}" />
                    <GridViewColumn Header="Id" Width="100" DisplayMemberBinding="{Binding processId}" />
                </GridView>
            </ListView.View>
        </ListView>

Expected results: - List of application process names with its Id loaded in the listview defined in my application

Actual results: - Nothing appears in the listview

You forgot to bind the processes to the listView control.

You should add:

listViewProcess.ItemSource = myProcess;

in the "Window_Loaded" method

You needs to set the main window DataContext and use INotifyPropertyChanged to to notify the view when the properties changed. Try this:

MainWindow.xaml.cs & myProcess model:

public partial class MainWindow : INotifyPropertyChanged
{
    private ObservableCollection<myProcess> _processes;
    public ObservableCollection<myProcess> Processes
    {
        get { return _processes; }
        set
        {
            if (value.Equals(_processes))
                return;

            _processes = value;
            OnPropertyChanged(nameof(Processes));
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }


    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var processes = Process.GetProcesses();
        Processes = new ObservableCollection<myProcess>();

        foreach (var item in processes)
            Processes.Add(new myProcess { processName = item.ProcessName, processId = item.Id });
    }


    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

}

public class myProcess
{
    public string processName { get; set; }
    public int processId { get; set; }
}

MainWindow.xaml:

<ListView ItemsSource="{Binding Processes}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" Width="200" DisplayMemberBinding="{Binding processName}" />
            <GridViewColumn Header="Id" Width="100" DisplayMemberBinding="{Binding processId}" />
        </GridView>
    </ListView.View>
</ListView>

if you don't want to use INotifyPropertyChanged , you can simply change your Window_Loaded like this:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var currentProcesses = Process.GetProcesses();
    var processes = new List<myProcess>();

    foreach (var item in currentProcesses)
        processes.Add(new myProcess { processName = item.ProcessName, processId = item.Id });

    listViewProcess.ItemsSource = processes;
}

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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