Daan Timmer
Daan Timmer

Reputation: 15057

GUI idea for WPF, though no clue how to implement it

What I've got is basicly two classes Plugin and PluginLauncher

Plugin is an abstract class that implements some functions to make a class a Plugin for my PluginLauncher class.

PluginLauncher is a class that holds a collection (SortedDictionary) including some helper functions to start, stop, restart all or a specific Plugin.

It also loads all plugins on initialisation. Each plugin can be a .exe or .dll with a class the inherits from Plugin. An AppDomain is created for each plugin and communication is also setup for each Plugin (through a simple IPC messaging through sockets) (still has to be implemented)

I want to have a very, VERY basic GUI implementation that just has a list of all loaded Plugins, noting the plugin name, its state (which can be running, stopping, stopped, stoppedprematurely (an Enum)) and a button per plugin to start, stop or restart it.

I know I can add this functionality programmaticaly by just placing the elements on the GUI and calculating each X/Y location etc. but I am sure WPF has some pre-made 'functionalities' for this. But I am quite new to WPF and have no clue where to start looking.

A simple note: I am limited to .net 3.5 (or lower) thus no 4.0 elements.

I've included a very simple (hooray mspaint skills) example of what I had in mind.

Simple MSPaint skills of what I had in mind

Upvotes: 1

Views: 228

Answers (2)

Kent Boogaart
Kent Boogaart

Reputation: 178790

The plugin nature of your app has little bearing on the mechanics of how you achieve this. Essentially, you need a collection of view models. Each item in that collection represents a plugin (but it could equally represent a customer or a chicken drumstick). You then bind an ItemsControl to this collection and define a template for how the item should be rendered.

Here is some pseudo-code to get you on your way:

public class PluginViewModel : ViewModel
{
    public string Name { get; }
    public PluginState State { get; private set; }
    public ICommand StartCommand { get; }
    public ICommand StopCommand { get; }
    public ICommand RestartCommand { get; }
}

public class PluginLauncherViewModel : ViewModel
{
    // use an ObservableCollection<PluginViewModel> to store your plugin view models
    public ICollection<PluginViewModel> Plugins { get; }
}

<ScrollViewer>
    <ItemsControl ItemsSource="{Binding Plugins}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                 <UniformGrid Rows="1">
                     <TextBlock Text="{Binding Name}"/>
                     <TextBlock Text="{Binding Status}"/>
                     <Button Command="{Binding StartCommand}">Start</Button>
                     <Button Command="{Binding StopCommand}">Stop</Button>
                     <Button Command="{Binding RestartCommand}">Restart</Button>
                 </UniformGrid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>

Some problems you will no doubt bump into:

  • the DataContext of the outer XAML (ie. the ScrollViewer in my above example) must be an instance of PluginLauncherViewModel. How you wire this up is up to you, and there are varied options. Start with something simple like setting it in your code-behind.
  • ViewModel is a base class for all view models. See here for an example.
  • Your implementation of ICommand should be MVVM friendly. See here for an example.

Upvotes: 5

Nano Taboada
Nano Taboada

Reputation: 4182

For the simplest approach you might consider the Table element. For a more fine-grained control I'd recommend you using a Grid.

Upvotes: 0

Related Questions