Jo

I am using ObjectDataProvider throughout my app and it works great as long as there is no parameters or parameter can be hard coded.

But I have a need to pass parameter to a method and allow it to take dynamic parameter as follows

<ObjectDataProvider x:Key="ExampleChart" ObjectType="{x:Type src:BLChart}" MethodName="GetCharts">

<ObjectDataProvider.MethodParameters>

<system:Int32>Person.ID</system:Int32>

</ObjectDataProvider.MethodParameters>

</ObjectDataProvider>

I have

this.Person = (Person)Application.Current.Properties["SelectedItem"];

in the constructor in the code behind.

the above code crashes. when I replace Person.ID with 35, it works great.

I know I am missing something. Any help is greatly appreciated.



Re: Windows Presentation Foundation (WPF) How to use ObjectDataProvider MethodName to pass dynamic parameters instead of hard coding parameters


Re: Windows Presentation Foundation (WPF) How to use ObjectDataProvider MethodName to pass dynamic parameters instead of hard coding parameters

Jo

Her example code used hard coded data, which works for me too.

I need to be able to pass dynamic data. So it is not of much help.





Re: Windows Presentation Foundation (WPF) How to use ObjectDataProvider MethodName to pass dynamic parameters instead of hard coding parameters

lee d

she binds the value in the textbox to one of the method prameters, if I am not mistaken




Re: Windows Presentation Foundation (WPF) How to use ObjectDataProvider MethodName to pass dynamic parameters instead of hard coding parameters

Jo

Her binding takes place after the method has been called and results returned which works for me too.

She is actually displaying her results I believe through binding.

What I need is to pass a value as a parameter to the calling method.





Re: Windows Presentation Foundation (WPF) How to use ObjectDataProvider MethodName to pass dynamic parameters instead of hard coding parameters

Paul Czywczynski

Doing what you want is not supported in markup in the current version. During a MS/WPF chat a while ago we brought up the issue and they said their aware of it and the feature might make it in a future version of WPF. Currently your only choice is to set the MethodParameters in code-behind.



Re: Windows Presentation Foundation (WPF) How to use ObjectDataProvider MethodName to pass dynamic parameters instead of hard coding parameters

Jo

Thank You so much. I am still researching hoping to find an answer.

Can you point me any sample C# code to do this in code behind.

Thanks





Re: Windows Presentation Foundation (WPF) How to use ObjectDataProvider MethodName to pass dynamic parameters instead of hard coding parameters

Paul Czywczynski

Sure no problem. Using part of your xaml above:

Code Snippet

ObjectDataProvider _odp = this.FindResource("ExampleChart")) as ObjectDataProvider; // "this" refers to the markup object whose resource has the ObjectDataProvider.
if (_odp != null)
{
using (_odp.DeferRefresh())
{
_odp.MethodParameters.Add(this.Person.ID);
}
}



You can also declare the whole thing in code-behind as well if you don't want to mix with markup.

Code Snippet

ObjectDataProvider _odp = new ObjectDataProvider();
using (_odp.DeferRefresh())
{
_odp.ObjectType = typeof(BLChart); // I am not sure what namespace "src" is mapped to so make sure to qualify this.
_odp.MethodName = "GetCharts";
_odp.MethodParameters.Add(this.Person.ID);
}
this.DataContext = _odp;






Re: Windows Presentation Foundation (WPF) How to use ObjectDataProvider MethodName to pass dynamic parameters instead of hard coding parameters

Jo

Awesome. I implemented your first solution. Worked great.

Thank you! Thank You!