I need to open same window for displaying reports, from many different pages, on button click. But (based on page where I open It) I need to provide which report should be displayed, title of window etc.
So I'm trying to create a global command with parameters for that, in order to avoid writing same button command in each ViewModel.
I know how to create global command:
public static class Global_commands
{
private static readonly RoutedUICommand _reports = new RoutedUICommand("View report", "View_report", typeof(Global_commands));
public static RoutedUICommand View_report
{
get{return _reports;}
}
}
//in App.xaml:
public App()
{
var view_report = new CommandBinding(Global_commands.View_report, View_Report_Executed, View_Report_CanExecute);
CommandManager.RegisterClassCommandBinding(typeof(Window), view_report);
}
private void View_Report_Executed(object sender, ExecutedRoutedEventArgs e)
{
//...
}
But that doesn't allow me to pass a parameter, or at least I don't know how. So I can't say which report should be displayed.
Another option for commands I know is this:
public class Register_command : ICommand
{
public event EventHandler CanExecuteChanged;
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
public Register_command(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
return true;
return _canExecute(parameter);
}
public void Execute(object parameter)
{
if (_execute != null)
_execute(parameter);
}
}
//in ViewModel of some window:
public ICommand Open_report { get; set; }
public SomeViewModel()
{
Open_report = new Register_command(Open_report_window, null);
}
//I would a method something like that - with parameters
private void Open_report_window(object parameter)
{
Report_Window report_wind = new Report_Window();
report_wind.Owner = System.Windows.Application.Current.MainWindow;
switch (parameter)
{
case "1":
report_wind.Title= "Report number 1";
report_wind.Report_Name="Report1.rdlc";
default:
break;
}
}
In this case, my Executed method allows me to pass a parameter, but I don't know how to pass this command globally.
Can somebody show me correct approach for this ?