Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# WPF PRISM – Pass multiple parameters to UserControl

I am using the PRISM framework for my C# WPF application. The UserControl is inheriting from the IDialogAware Interface. What I’m trying to do, is passing multiple parameters from the MainWindowViewModel to the UserControlViewModel. But when i the OnDialogOpened Method is called, and I’m trying to fill the parameter1, there is an System.FormatException because the value I’m getting from parameters.GetValue("parameter1") is:

"false, parameter2=False"

Sure, I could solve it with the substring() method, but isn’t there a nicer or better way?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

MainWindowViewModel:

_dialogService.ShowDialog("SecondUserControl", 
        new DialogParameters($"parameter1={parameter1}, parameter2={parameter2}"), r =>
        {});

UserControlViewModel:

public void OnDialogOpened(IDialogParameters parameters)
{
  var parameter1 = parameters.GetValue<bool>("parameter1");
  var parameter2 = parameters.GetValue<bool>("parameter2");
}

>Solution :

DialogParameters inherits ParametersBase which implements IEnumerable<KeyValuePair<string, object>> and has methods typical for collections like Add(string key, object value) so you can create new instance and insert your parameters or just initialize a new instance with collection initializer. I found an example in "Using dialog service" par of official documentation:

var parameters = new DialogParameters();
parameters.Add("parameter1", value1);
parameters.Add("parameter2", value2);

// or

var parameters = new DialogParameters
    {
        { "parameter1", value1 },
        { "parameter2", value2 }
    };

_dialogService.ShowDialog("SecondUserControl", parameters, r => {});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading