Commits

Mark Heath committed f620e63

starting to make the config window editable (WIP)

Comments (0)

Files changed (6)

MusicRater.Tests/ToolbarControlTests.cs

             return true;
         }
 
-        public event EventHandler CanExecuteChanged;
+        public event EventHandler CanExecuteChanged = (o,e) => {};
 
         public void Execute(object parameter)
         {

MusicRater/MusicRater.csproj

     <Compile Include="DummyViewModels\DummyConfigWindowViewModel.cs" />
     <Compile Include="DummyViewModels\DummyRatingControlViewModel.cs" />
     <Compile Include="DummyViewModels\DummyTrackListControlViewModel.cs" />
+    <Compile Include="MvvmUtils\DialogCloser.cs" />
     <Compile Include="Persistence\IIsolatedStore.cs" />
     <Compile Include="Persistence\IsolatedStore.cs" />
     <Compile Include="MvvmUtils\BoolToVisibilityConverter.cs" />
     <Compile Include="DummyViewModels\DummyErrorMessageViewModel.cs" />
     <Compile Include="DummyViewModels\DummyMainWindowViewModel.cs" />
     <Compile Include="DummyViewModels\DummyPositionControlViewModel.cs" />
+    <Compile Include="ViewModels\ConfigWindowViewModel.cs" />
     <Compile Include="Views\ConfigWindow.xaml.cs">
       <DependentUpon>ConfigWindow.xaml</DependentUpon>
     </Compile>

MusicRater/MvvmUtils/DialogCloser.cs

+using System.Windows;
+using System.Windows.Controls;
+
+namespace MusicRater.MvvmUtils
+{
+    public static class DialogCloser
+    {
+        public static readonly DependencyProperty DialogResultProperty =
+            DependencyProperty.RegisterAttached(
+                "DialogResult",
+                typeof(bool?),
+                typeof(DialogCloser),
+                new PropertyMetadata(DialogResultChanged));
+
+        private static void DialogResultChanged(
+            DependencyObject d,
+            DependencyPropertyChangedEventArgs e)
+        {
+            var window = d as ChildWindow;
+            if (window != null)
+                window.DialogResult = e.NewValue as bool?;
+        }
+        public static void SetDialogResult(ChildWindow target, bool? value)
+        {            
+            target.SetValue(DialogResultProperty, value);
+        }
+        public static bool? GetDialogResult(ChildWindow target)
+        {
+            return (bool?)target.GetValue(DialogResultProperty);
+        }
+
+    }
+}

MusicRater/ViewModels/ConfigWindowViewModel.cs

+using System.Collections.Generic;
+using System.Linq;
+using GalaSoft.MvvmLight;
+using System.Windows.Input;
+using System;
+
+namespace MusicRater.ViewModels
+{
+    public class ConfigWindowViewModel : ViewModelBase
+    {
+        private readonly List<Criteria> originalCriteria;
+        private readonly List<Criteria> editCriteria;
+        private readonly ICommand updateCommand;
+        private bool? dialogResult;
+
+        public ConfigWindowViewModel(IEnumerable<Criteria> criteria)
+        {
+            this.originalCriteria = new List<Criteria>(criteria);
+            this.editCriteria = (from c in originalCriteria select Clone(c)).ToList();
+            this.updateCommand = new UpdateCriteriaCommand(originalCriteria, editCriteria, (dr) => this.DialogResult = dr);
+        }
+
+        private Criteria Clone(Criteria criteria)
+        {
+            return new Criteria(criteria.Name, criteria.Weight);
+        }
+
+        public bool? DialogResult
+        {
+            get
+            {
+                return dialogResult;
+            }
+            set
+            {
+                if (dialogResult != value)
+                {
+                    dialogResult = value;
+                    RaisePropertyChanged("DialogResult");
+                }
+            }
+        }
+        public ICommand UpdateCommand { get { return this.updateCommand; } }
+
+        public List<Criteria> Criteria { get { return editCriteria; }  }
+    }
+
+    class UpdateCriteriaCommand : ICommand
+    {
+        private readonly List<Criteria> original;
+        private readonly List<Criteria> edited;
+        private readonly Action<bool?> updateDialogResult;
+
+        public UpdateCriteriaCommand(List<Criteria> original, List<Criteria> edited, Action<bool?> updateDialogResult)
+        {
+            this.original = original;
+            this.edited = edited;
+            this.updateDialogResult = updateDialogResult;
+        }
+
+        public bool CanExecute(object parameter)
+        {
+            return true;
+        }
+
+        public event EventHandler CanExecuteChanged = (o, e) => { };
+
+        public void Execute(object parameter)
+        {
+            for (int n = 0; n < this.original.Count; n++)
+            {
+                //this.original[n].Name = this.edited[n].Name;
+                //this.original[n].Weight = this.edited[n].Weight;
+            }
+            this.updateDialogResult(true);
+        }
+    }
+}

MusicRater/Views/ConfigWindow.xaml

            xmlns:dummy="clr-namespace:MusicRater.DummyViewModels"
            d:DataContext="{Binding Source={StaticResource viewModel}}"           
            Width="400" Height="300" 
+           xmlns:my="clr-namespace:MusicRater.MvvmUtils"
+           my:DialogCloser.DialogResult="{Binding DialogResult}"
            Title="ConfigWindow">
     <controls:ChildWindow.Resources>
         <dummy:DummyConfigWindowViewModel x:Key="viewModel" />
                 </DataTemplate>
             </ItemsControl.ItemTemplate>
         </ItemsControl>
-        <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
-        <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
+        <Button x:Name="CancelButton" Command="{Binding UpdateCommand}" Content="Cancel" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
+        <Button x:Name="OKButton" Command="{Binding CancelCommand}" Content="OK" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
     </Grid>
 </controls:ChildWindow>

MusicRater/Views/ConfigWindow.xaml.cs

         {
             InitializeComponent();
         }
-
-        private void OKButton_Click(object sender, RoutedEventArgs e)
-        {
-            this.DialogResult = true;
-        }
-
-        private void CancelButton_Click(object sender, RoutedEventArgs e)
-        {
-            this.DialogResult = false;
-        }
     }
 }