Commits

Christian Specht committed be5e075 Draft

bind FireCommand (=run CommandSet) to buttons

The RelayCommand class was taken from here:
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Comments (0)

Files changed (4)

src/MissileSharp.Launcher/MissileSharp.Launcher.csproj

       <DependentUpon>App.xaml</DependentUpon>
       <SubType>Code</SubType>
     </Compile>
+    <Compile Include="RelayCommand.cs" />
     <Compile Include="Services\ConfigService.cs" />
     <Compile Include="Services\IConfigService.cs" />
     <Compile Include="Services\IMessageService.cs" />

src/MissileSharp.Launcher/RelayCommand.cs

+using System;
+using System.Diagnostics;
+using System.Windows.Input;
+
+public class RelayCommand : ICommand
+{
+    #region Fields
+
+    readonly Action<object> _execute;
+    readonly Predicate<object> _canExecute;
+
+    #endregion // Fields
+
+    #region Constructors
+
+    public RelayCommand(Action<object> execute)
+        : this(execute, null)
+    {
+    }
+
+    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
+    {
+        if (execute == null)
+            throw new ArgumentNullException("execute");
+
+        _execute = execute;
+        _canExecute = canExecute;
+    }
+    #endregion // Constructors
+
+    #region ICommand Members
+
+    [DebuggerStepThrough]
+    public bool CanExecute(object parameter)
+    {
+        return _canExecute == null ? true : _canExecute(parameter);
+    }
+
+    public event EventHandler CanExecuteChanged
+    {
+        add { CommandManager.RequerySuggested += value; }
+        remove { CommandManager.RequerySuggested -= value; }
+    }
+
+    public void Execute(object parameter)
+    {
+        _execute(parameter);
+    }
+
+    #endregion // ICommand Members
+}

src/MissileSharp.Launcher/ViewModels/MainWindowViewModel.cs

 using System.Collections.ObjectModel;
 using System.ComponentModel;
 using System.Windows;
+using System.Windows.Input;
 using MissileSharp.Launcher.Services;
 
 namespace MissileSharp.Launcher.ViewModels
 
         public ObservableCollection<string> CommandSets { get; set; }
 
+        public ICommand FireCommand { get; set; }
+
         public event PropertyChangedEventHandler PropertyChanged;
 
         public MainWindowViewModel(IConfigService configService, IMessageService messageService)
             this.configService = configService;
             this.messageService = messageService;
 
+            this.FireCommand = new RelayCommand(new Action<object>(this.FireMissile));
+
             try
             {
                 model.LoadCommandSets(configService.GetConfig());
 
             this.CommandSets = new ObservableCollection<string>(model.GetLoadedCommandSetNames());
         }
+
+        private void FireMissile(Object obj)
+        {
+            this.model.RunCommandSet(obj.ToString());
+        }
     }
 }

src/MissileSharp.Launcher/Views/MainWindow.xaml

         <ListBox ItemsSource="{Binding CommandSets}">
             <ListBox.ItemTemplate>
                 <DataTemplate>
-                    <Button Content="{Binding}" />
+                    <Button Content="{Binding}" Command="{Binding DataContext.FireCommand, RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding}" />
                 </DataTemplate>
             </ListBox.ItemTemplate>
         </ListBox>