数据绑定(七)使用ObjectDataProvider对象作为Binding的Source
原文:数据绑定(七)使用ObjectDataProvider对象作为Binding的Source
ObjectDataProvider就是把对象作为数据源提供给Binding,类似的还有XmlDataProvider,也就是把XML数据作为数据源提供给Binding,这两个类的父类都是DataSourceProvider抽象类
举例
有一个Calculator类,提供了一个Add方法
public string Add(string arg1, string arg2)
{
double x = 0;
double y = 0;
double z = 0;
if (double.TryParse(arg1, out x) && double.TryParse(arg2, out y))
{
z = x + y;
return z.ToString();
}
return "Error";
}
界面代码很简单,有三个输入框
<TextBox x:Name="textBoxArg1" Margin="5" />
<TextBox x:Name="textBoxArg2" Margin="5" />
<TextBox x:Name="textBoxResult" Margin="5" />
后台代码:
ObjectDataProvider odp = new ObjectDataProvider();
odp.ObjectInstance = new Calculator();
odp.MethodName = "Add";
odp.MethodParameters.Add("0");
odp.MethodParameters.Add("0"); Binding bindingToArg1 = new Binding();
bindingToArg1.Source = odp;
bindingToArg1.Path = new PropertyPath("MethodParameters[0]");
bindingToArg1.BindsDirectlyToSource = true;
bindingToArg1.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; Binding bindingToArg2 = new Binding();
bindingToArg2.Source = odp;
bindingToArg2.Path = new PropertyPath("MethodParameters[1]");
bindingToArg2.BindsDirectlyToSource = true;
bindingToArg2.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; Binding bindingToResult = new Binding();
bindingToResult.Source = odp;
bindingToResult.Path = new PropertyPath("."); textBoxArg1.SetBinding(TextBox.TextProperty, bindingToArg1);
textBoxArg2.SetBinding(TextBox.TextProperty, bindingToArg2);
textBoxResult.SetBinding(TextBox.TextProperty, bindingToResult);
首先定义了一个ObjectDataProvider对象并与Calculator类的Add方法进行关联,把第一个输入框中的内容同函数的参数1进行绑定,第二个输入框中的内容同函数的参数2进行绑定,函数的返回值与第三个输入框绑定,BindsDirectlyToSource = true这句代码的意思是,Binding对象只负责把从UI元素收集到的数据写入其直接Source(即ObjectDataProvider对象)而不是被ObjectDataProvider对象包装着的Calculator对象,
数据绑定(七)使用ObjectDataProvider对象作为Binding的Source的更多相关文章
- 数据绑定(四)使用DataContext作为Binding的Source
原文:数据绑定(四)使用DataContext作为Binding的Source DataContext属性被定义在FrameworkElement类里,这个类是WPF控件的基类,这意味着所有WPF控件 ...
- 数据绑定(六)使用XML数据作为Binding的Source
原文:数据绑定(六)使用XML数据作为Binding的Source .NET Framework提供了两套处理XML数据的类库 1. 符合DOM标准的类库:包括XmlDocument.XmlEleme ...
- 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合
[源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...
- 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合
背水一战 Windows 10 之 绑定 通过 Binding 绑定对象 通过 x:Bind 绑定对象 通过 Binding 绑定集合 通过 x:Bind 绑定集合 示例1.演示如何通过 Bindin ...
- 七牛云免费对象存储,并绑定到cloudreve中
之前开通了腾讯云的对象存储COS并使用中,不过之前主要将它当作云盘使用,这两天再做博客系统时发现也可以将它作为网站的图库,这样对网站的访问效率也会提高. 今天了解到七牛云有免费的对象存储可以使用,于是 ...
- 解读经典《C#高级编程》第七版 Page79-93.对象和类型.Chapter3
前言 本篇我们继续讲解本章其余的部分:构造函数.只读字段.匿名类型.结构详解.部分类.静态类.Object类.扩展方法,等. 01 类 构造函数 构造函数是一种特殊的方法: 与类同名 没有返回值,甚至 ...
- Kendo MVVM 数据绑定(七) Invisible/Visible
Kendo MVVM 数据绑定(七) Invisible/Visible Invisible/Visible 绑定可以根据 ViewModel 的某个属性来显示/隐藏 DOM 元素.例如: <d ...
- php利用七牛云的对象存储完成图片上传-高效管理图片
在搭建个人博客时,大家都会买一台云服务器.可是图片的存放一直是一个问题,冷月帮大家找到一个免费的第三方平台对象存储-七牛云.大家可以把图片上传到七牛云的对象存储,大大节约服务器的压力. 首先,大家在使 ...
- 一起写框架-MVC框架-基础功能-Date类型数据绑定(七)
实现功能 表单请求传递的数据,格式为以下格式的日期时间数据. (1):yyyy-MM-dd hh:mm:ss (2):yyyy-MM-dd 执行方法可以使用Date类型接收. 实现思路 1.获得表单字 ...
随机推荐
- js进阶 11-14 jquery如何实现元素的替换和遍历
js进阶 11-14 jquery如何实现元素的替换和遍历 一.总结 一句话总结:替换:replaceAll() 与 replaceWith().遍历:each(). 1.replaceAll() ...
- AJAX 相关笔记
AJAX (Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)). 平时工作中使用ajax的频率挺高的,这里整理了一些ajax相关的小知识,后续 ...
- jquery-3 jquery选择器
jquery-3 jquery选择器 一.总结 一句话总结:选择器使用的时候可以结合参考手册,里面都已经详细归类好了.(多用才能熟啊) 1.$(:input).css({'color':'#98765 ...
- 【codeforces 755D】PolandBall and Polygon
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 理解Erlang/OTP Supervisor
http://www.cnblogs.com/me-sa/archive/2012/01/10/erlang0030.html Supervisors are used to build an hie ...
- WPF+SignalR实现用户列表实时刷新
原文:WPF+SignalR实现用户列表实时刷新 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/lordwish/article/details/5 ...
- 一个完整的Erlang应用
http://blog.chinaunix.net/uid-25876834-id-3308693.html 这里介绍构建一个完整的Erlang/OTP应用的例子,最后还给出了一个在实际生成环境中,如 ...
- [GeekBand] STL Traits 使用简介
本文参考文献::GeekBand课堂内容,授课老师:张文杰 :C++ Templates 15章节 :网络资料: http://blog.csdn.net/my_business/article/d ...
- sparksql dataset
java /** *2.0之后使用sparksession即可,不需要再去创建sqlcontext *@author Tele * */ public class Demo { private sta ...
- uml 类图依赖与关联的区别
类与类之间由弱到强关系是: 没关系 < 依赖 < 关联 < 聚合 < 组合. 1.依赖(dependency) 可以简单的理解,就是一个类A使用到了另一个类B,而这种使用 ...