开发者

Problem Binding array of Processes to WPF ListBox

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,开发者_如何学C 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.


I am wondering why you are getting an output at all.

ItemsSource="{Binding Path=ProcessName}"

Means the source of items is the ProcessName. The name is a string and a string is an IEnumerable<Char>. So the listbox displays each char as one entry.

You have to set the ItemsSource to the list itself. In your case it would be

ItemsSource="{Binding}"

Because the ProcessList is already the data context of your grid, which gets inherited to the listbox.

If you are not using a view model class (which is best practice when working with the MVVM pattern) you can do a DataContext = this; in your windows constructor to set the data context of the window to its own code-behind. If you do this the binding of your list would change to

ItemsSource="{Binding procList}"

However you should introduce a property and implement INotifyPropertyChanged so your UI is notified and updated when the datasource changes.

TL;DR version: taking a look at guides to XAML/Bindings and the MVVM Pattern is required if you want to take use full use of WPF.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜