How to pass value to ObjectDataProvider.MethodParameters dynamically in runtime
Supply some default value to the DataProvider so that it s already set up and bound to your UI.
Accept a value from user at runtime and then supply that to the data provider using FindResource call and refresh...
var myValue = GetFromUser(); ((ObjectDataProvider)this.FindResource("ADUsers")).MethodParameters.Clear(); ((ObjectDataProvider)this.FindResource("ADUsers")).MethodParameters.Add(myValue); ((ObjectDataProvider )this.FindResource("ADUsers")).Refresh();
Or another tricky way is to OneWayToSource binding with MethodParameters...
<TextBox x:Name="UserInput"> <TextBox.Text> <Binding Source="{StaticResource ADUsers}" Path="MethodParameters[0]" BindsDirectlyToSource="True" Mode="OneWayToSource"> </Binding> </TextBox.Text> </TextBox>
But for this to work your TextBox Text (string) must be matched to the type of the parameter which sadly in our case is integer.In order to fix that create a converter that will take care of this issue.
public class IntToStringConverter : IValueConverter{ public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value.ToString(); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { int intValue = 0; string strText = value?.ToString(); if (!string.IsNullOrEmpty(strText)) { intValue = int.Parse(strText); } return intValue; } }