原文:数据绑定(七)使用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的更多相关文章

  1. 数据绑定(四)使用DataContext作为Binding的Source

    原文:数据绑定(四)使用DataContext作为Binding的Source DataContext属性被定义在FrameworkElement类里,这个类是WPF控件的基类,这意味着所有WPF控件 ...

  2. 数据绑定(六)使用XML数据作为Binding的Source

    原文:数据绑定(六)使用XML数据作为Binding的Source .NET Framework提供了两套处理XML数据的类库 1. 符合DOM标准的类库:包括XmlDocument.XmlEleme ...

  3. 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合

    [源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...

  4. 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合

    背水一战 Windows 10 之 绑定 通过 Binding 绑定对象 通过 x:Bind 绑定对象 通过 Binding 绑定集合 通过 x:Bind 绑定集合 示例1.演示如何通过 Bindin ...

  5. 七牛云免费对象存储,并绑定到cloudreve中

    之前开通了腾讯云的对象存储COS并使用中,不过之前主要将它当作云盘使用,这两天再做博客系统时发现也可以将它作为网站的图库,这样对网站的访问效率也会提高. 今天了解到七牛云有免费的对象存储可以使用,于是 ...

  6. 解读经典《C#高级编程》第七版 Page79-93.对象和类型.Chapter3

    前言 本篇我们继续讲解本章其余的部分:构造函数.只读字段.匿名类型.结构详解.部分类.静态类.Object类.扩展方法,等. 01 类 构造函数 构造函数是一种特殊的方法: 与类同名 没有返回值,甚至 ...

  7. Kendo MVVM 数据绑定(七) Invisible/Visible

    Kendo MVVM 数据绑定(七) Invisible/Visible Invisible/Visible 绑定可以根据 ViewModel 的某个属性来显示/隐藏 DOM 元素.例如: <d ...

  8. php利用七牛云的对象存储完成图片上传-高效管理图片

    在搭建个人博客时,大家都会买一台云服务器.可是图片的存放一直是一个问题,冷月帮大家找到一个免费的第三方平台对象存储-七牛云.大家可以把图片上传到七牛云的对象存储,大大节约服务器的压力. 首先,大家在使 ...

  9. 一起写框架-MVC框架-基础功能-Date类型数据绑定(七)

    实现功能 表单请求传递的数据,格式为以下格式的日期时间数据. (1):yyyy-MM-dd hh:mm:ss (2):yyyy-MM-dd 执行方法可以使用Date类型接收. 实现思路 1.获得表单字 ...

随机推荐

  1. Stacks of Flapjacks

    Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data s ...

  2. $.ajax 请求 拦截器 重定向 无效 解决办法

    在ajax 异步请求下 拦截器过滤器中使用 重定向 页面响应无效 我这里用的是springboot框架,用拦截器实现 对请求的拦截 ,session超时直接跳转到login.html页面. 后台代码: ...

  3. AE控制图层中要素可见状态的几种方法

    转自原文 AE控制图层中要素可见状态的几种方法 工作中常有这样的需求,一个作业图层由几个作业员来操作,我们要 控制每一个作业员只能看到他负责的区域.作业员的可见区域控制有时候是按空间区域划分,有时候是 ...

  4. [React] Use React ref to Get a Reference to Specific Components

    When you are using React components you need to be able to access specific references to individual ...

  5. Extended paging tables to map guest physical memory addresses from virtual memory page tables to host physical memory addresses in a virtual machine system

    A processor including a virtualization system of the processor with a memory virtualization support ...

  6. 【u219】最长链

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 现给出一棵N个结点二叉树,问这棵二叉树中最长链的长度为多少,保证了1号结点为二叉树的根. [提示] 关 ...

  7. 并发编程--CAS自旋锁

    在前两篇博客中我们介绍了并发编程--volatile应用与原理和并发编程--synchronized的实现原理(二),接下来我们介绍一下CAS自旋锁相关的知识. 一.自旋锁提出的背景 由于在多处理器系 ...

  8. java及jdbc与sql之间日期的转换

    javaSE中主要为日期字符串和日期对象之间的转换 JDBC中主要是util中Date与sql中作为数据库中Date的转换 public class DateDemo { public static ...

  9. 在CentOS系统上将deb包转换为rpm包

    转载自 http://www.heminjie.com/system/linux/1487.html deb文件格式本是ubuntu/debian系统下的安装文件,那么我想要在redhat/cento ...

  10. 如何在华为云软件开发云上运行Python

    一. 华为云软件开发云与Python 1. 华为云软件开发云简介 华为云软件开发云(DevCloud)是集华为近30年研发实践,前沿研发理念,先进研发工具为一体的一站式云端DevOps平台,面向开发者 ...