using MvvmDialog1.View;
using MvvmDialog1.ViewModel.Interfaces;
using MvvmDialog1.ViewModel.Utils;
using MvvmDialog1.ViewModel.ViewModels;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace MvvmDialog1
{
///
/// Interaction logic for App.xaml
///
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
IDialogService dialogService = new DialogService(MainWindow);
dialogService.Register
dialogService.Register
var viewModel = new MainWindowViewModel(dialogService);
var view = new MainWindow { DataCOntext= viewModel };
view.ShowDialog();
}
}
}
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStartupLocation="CenterScreen"
Title="ShowDialogWindow" Height="190.036" Width="300">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStartupLocation="CenterScreen"
Title="MainWindow" Height="350" Width="525">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStartupLocation="CenterScreen"
Title="ShowWindow" Height="190.036" Width="300">
VerticalAlignment="Top" Width="225"/>
using System.ComponentModel;
namespace MvvmDialog1.ViewModel.Utils
{
abstract class BaseViewModel : INotifyPropertyChanged
{
protected void OnPropertyChanged(string property)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
using MvvmDialog1.ViewModel.Interfaces;
using MvvmDialog1.ViewModel.Utils;
using System;
using System.Windows.Input;
namespace MvvmDialog1.ViewModel.ViewModels
{
class DialogWindowViewModel : IDialogRequestClose
{
public string Message { get; set; }
///
/// Komenda zatwierdzająca (zwrócenie wyniku "prawda") i zamykająca okno.
///
public ICommand OkCommand { get; private set; }
///
/// Komenda zamykająca okno ze zwróceniem wyniku "fałsz".
///
public ICommand CancelCommand { get; private set; }
///
/// Domyślny konstruktor wymagany przez Xaml.
///
public DialogWindowViewModel() { }
///
/// Właściwy konstruktor stosowany przy tworzeniu instancji okna.
///
/// Wiadomość wyświetlana w oknie. Zamiast tego mogą być jakiekolwiek inne zmienne.
public DialogWindowViewModel(string message)
{
this.Message = message;
this.OkCommand = new RelayCommand(x => CloseRequested.Invoke(this, new DialogCloseRequestedEventArgs(true)));
this.CancelCommand = new RelayCommand(x => CloseRequested.Invoke(this, new DialogCloseRequestedEventArgs(false)));
}
public event EventHandler
}
}
using System.Windows;
namespace MvvmDialog1.ViewModel.Interfaces
{
interface IDialog
{
object DataContext { get; set; }
bool? DialogResult { get; set; }
Window Owner { get; set; }
void Close();
bool? ShowDialog();
void Show();
}
}
using MvvmDialog1.ViewModel.Utils;
using System;
namespace MvvmDialog1.ViewModel.Interfaces
{
interface IDialogRequestClose
{
event EventHandler
}
}
namespace MvvmDialog1.ViewModel.Interfaces
{
interface IDialogService
{
void Register
where TViewModel : IDialogRequestClose
where TView : IDialog;
bool? ShowDialog
void Show
}
}
using MvvmDialog1.ViewModel.Interfaces;
using MvvmDialog1.ViewModel.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace MvvmDialog1.ViewModel.ViewModels
{
class MainWindowViewModel : BaseViewModel
{
string _message;
readonly IDialogService _dialogService;
public ICommand DisplayDialogWindow { get; private set; }
public ICommand DisplayWindow { get; private set; }
public string Message
{
get
{
return this._message;
}
set
{
this._message = value;
this.OnPropertyChanged("Message");
}
}
public MainWindowViewModel() { }
public MainWindowViewModel(IDialogService dialogService)
{
this._dialogService = dialogService;
this.DisplayDialogWindow = new RelayCommand(x => displayMessage());
this.DisplayWindow = new RelayCommand(x => display());
}
private void displayMessage()
{
var viewModel = new DialogWindowViewModel("Hello");
bool? result = this._dialogService.ShowDialog(viewModel);
if (result.HasValue)
{
if (result.Value)
{
this.Message = viewModel.Message;
this.OnPropertyChanged("Message");
}
else
{
this.Message = "Cancel";
this.OnPropertyChanged("Message");
}
}
}
private void display()
{
var viewModel = new WindowViewModel("Hello");
this._dialogService.Show(viewModel);
}
}
}
using System;
namespace MvvmDialog1.ViewModel.Utils
{
class DialogCloseRequestedEventArgs : EventArgs
{
public bool? DialogResult { get; private set; }
public DialogCloseRequestedEventArgs(bool? dialogResult)
{
this.DialogResult = dialogResult;
}
}
}
using MvvmDialog1.ViewModel.Interfaces;
using System;
using System.Collections.Generic;
using System.Windows;
namespace MvvmDialog1.ViewModel.Utils
{
class DialogService : IDialogService
{
readonly Window _owner;
public IDictionary
public DialogService(Window owner)
{
this._owner = owner;
this.Mappings = new Dictionary
}
public void Register
where TViewModel : IDialogRequestClose
where TView : IDialog
{
if (this.Mappings.ContainsKey(typeof(TViewModel)))
{
throw new ArgumentException("Type " + typeof(TViewModel) + " is already mapped to type " + typeof(TView));
}
this.Mappings.Add(typeof(TViewModel), typeof(TView));
}
public bool? ShowDialog
{
return getDialog
}
public void Show
{
getDialog
}
private IDialog getDialog
{
Type viewType = Mappings[typeof(TViewModel)];
IDialog dialog = (IDialog)Activator.CreateInstance(viewType);
EventHandler
handler = (sender, e) =>
{
viewModel.CloseRequested -= handler;
if (e.DialogResult.HasValue)
{
dialog.DialogResult = e.DialogResult;
}
else
{
dialog.Close();
}
};
viewModel.CloseRequested += handler;
dialog.DataCOntext= viewModel;
dialog.Owner = _owner;
return dialog;
}
}
}
using System;
using System.Windows.Input;
namespace MvvmDialog1.ViewModel.Utils
{
class RelayCommand : ICommand
{
readonly Action _method;
readonly Predicate _condition;
public RelayCommand(Action method) : this(method, null) { }
public RelayCommand(Action method, Predicate condition)
{
this._cOndition= condition;
this._method = method;
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
public bool CanExecute(object parameter)
{
return this._cOndition== null ? true : this._condition(parameter);
}
public void Execute(object parameter)
{
this._method(parameter);
}
public void Execute()
{
this._method(null);
}
}
}
using MvvmDialog1.ViewModel.Interfaces;
using MvvmDialog1.ViewModel.Utils;
using System;
using System.Windows;
using System.Windows.Input;
namespace MvvmDialog1.ViewModel.ViewModels
{
class WindowViewModel : BaseViewModel, IDialogRequestClose
{
string _message;
public string Message
{
get
{
return this._message;
}
set
{
this._message = value;
this.OnPropertyChanged("Message");
}
}
///
/// Domyślny konstruktor wymagany przez Xaml.
///
public WindowViewModel() { }
///
/// Właściwy konstruktor stosowany przy tworzeniu instancji okna.
///
/// Wiadomość wyświetlana w oknie. Zamiast tego mogą być jakiekolwiek inne zmienne.
public WindowViewModel(string message)
{
this.Message = message;
}
public event EventHandler
}
}