如何在运行时动态地将值传递给 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)值(从用户那里获取)。我怎样才能做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
向 DataProvider 提供一些默认值,以便它已经设置并绑定到您的 UI。
在运行时接受用户的值,然后使用 FindResource 调用将其提供给数据提供者并刷新...
或者另一种棘手的方法是将 OneWayToSource 与 MethodParameters 绑定...
但是要使此方法起作用,您的TextBox 文本(字符串)必须与参数的类型匹配,遗憾的是在我们的例子中是整数。
为了解决这个问题,创建一个转换器来解决这个问题。
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...
Or another tricky way is to OneWayToSource binding with MethodParameters...
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.