如何在运行时动态地将值传递给ObjectDataProvider.MethodParameters [英] How to pass value to ObjectDataProvider.MethodParameters dynamically in runtime

查看:164
本文介绍了如何在运行时动态地将值传递给ObjectDataProvider.MethodParameters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码:

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

和:

<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>

我想动态传递我的参数(这里是20)值(获取fron用户).我该怎么办?

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

推荐答案

  1. 为DataProvider提供一些默认值,以便它已经设置并绑定到您的UI.

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

在运行时接受用户的值,然后使用FindResource调用并刷新将其提供给数据提供者...

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();

或者另一种棘手的方法是使用MethodParameters ...来绑定OneWayToSource ...

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>

但是要使其正常工作,您的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;
    } 
}

这篇关于如何在运行时动态地将值传递给ObjectDataProvider.MethodParameters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆