Ok, so I am trying to implement a fairly straightforward feature in my app. I want to have a list of processes running (listed by their process name, ID, whatever) and when I click on a process in the list, update a few labels with information on that process (again, process name, ID, stuff like that).
After having a look at MSDN docs for DataBinding with Collections, I created my own custom class ProcessList that inherits from ObservableCollection. The class is completely bare now, I had previously been messing around with it trying to get things to work so I guess I may as well just be working straight with an ObservableCollection, but anyway. Here is what I have at the moment:
public partial class MainWindow : Window
{
private Dictionary<int, Injector> injectors;
public Process[] processes;
public ProcessList procList;
public MainWindow()
{
procList = new ProcessList();
foreach (var p in Process.GetProcesses())
{
procList.Add(p);
}
InitializeComponent();
processGrid.DataContext = procList;
}
private void button_refresh_processes_Click(object sender, RoutedEventArgs e)
{
processes = Process.GetProcesses();
}
}
}
And the pertinent XAML:
<Grid Name="processGrid" Margin="0,21,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="348*" />
<ColumnDefinition Width="145*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="147*" />
<RowDefinition Height="42*" />
</Grid.RowDefinitions>
<Button Content="Inject" Grid.Row="1" Height="23" HorizontalAlignment="Center" Margin="0,0,0,0" Name="button_inject" VerticalAlignment="Center" Width="75" />
<ListBox Height="147" HorizontalAlignment="Left" Name="process_list" VerticalAlignment="Top" Width="348" ItemsSource="{Binding Path=ProcessName}" />
<Grid Grid.Column="1" Height="147" HorizontalAlignment="Left" Name="grid2" VerticalAlignment="Top" Width="145">
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Label Content="Process ID:" Grid.RowSpan="1" Height="28" HorizontalAlignment="Left" Margin="0" Name="label_pid" VerticalAlignment="Center" />
<Label Content="PID" Grid.Column="1" Grid.RowSpan="1" Height="28" HorizontalAlignment="Right" Margin="0" Name="label_pid_value" VerticalAlignment="Center" />
</Grid>
<Button Content="Refresh" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="6,9,0,0" Name="button_refresh_processes" VerticalAlignment="Top" Width="75" Click="button_refresh_processes_Click" />
</Grid>
The problem I am currently having is that instead of displaying a list of all currently running processes by their ProcessName, it instead lists what appears to be each character from the first process's name as seperate entries in the list, and nothing more. Screenshot
I have no idea what is going on, though I'm sure I'm doing something extremely stupid as I am very new to WPF development and Data Binding.