How to pass value to ObjectDataProvider.MethodParameters dynamically in runtime

浪子不回头ぞ 提交于 2019-12-21 17:22:00

问题


I wrote this code:

public class CustomData
{
    public int F1 { get; set; }
    public int F2 { get; set; }
    public string F3 { get; set; }
}


public class RetrievCustomData : List<CustomData>
{
    public RetrievCustomData GetSome(int i)
    {
        for (int j = 0; j < i; j++)
        {
            CustomData cd = new CustomData();
            Random rnd = new Random();
            cd.F1 = j;
            cd.F2 = rnd.Next(i);
            cd.F3 = "nima";
            this.Add(cd);
        }

        return this;
    }
}

and:

<Window.Resources>
    <ObjectDataProvider x:Key="ADUsers" ObjectType="{x:Type src:RetrievCustomData}"
                MethodName="GetSome">
        <ObjectDataProvider.MethodParameters>
            <sys:Int32>20</sys:Int32>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

I want to pass my parameter (here is 20) value dynamically (get fron user). How I can do this?


回答1:


  1. Supply some default value to the DataProvider so that it s already set up and bound to your UI.

  2. 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;
    } 
}


来源:https://stackoverflow.com/questions/6882462/how-to-pass-value-to-objectdataprovider-methodparameters-dynamically-in-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!