ObjectDataProvider 提供了绑定任意. net 类型的功能, 具体功能如下:
1.ObjectDataProvider 提供了绑定任意 CLR 类型的公嫩那个.
2. 它可以再 XAML 中利用生命史的语言以及参数化的构造函数完成对数据的创建
3. 增加对成员函数的绑定
4. 提供了更多的异步绑定的功能
下面用一个加法计算器来进行实例说明:
请先看我们的加法类:
- C# 代码
- namespace BindingDemo
- {
- public class Calculator
- {public double Add(double one,double two)
- {
- return one + two;
- }
- public string Add(string arg1, string arg2)
- {
- int x = 0;
- int y = 0;
- if (int.TryParse(arg1, out x) && int.TryParse(arg2, out y))
- {
- return this.Add(x, y).ToString();
- }
- else
- {
- return "Input Error!";
- }
- }
- }
- }
接下来是 XAML 文件的定义:
Xaml 代码
- <Window x:Class="BindingDemo.Window1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:BindingDemo"
- xmlns:system="clr-namespace:System;assembly=mscorlib"
- Title="Add" Height="300" Width="300">
- <Window.Resources>
- <ObjectDataProvider x:Key="odp" ObjectType="{x:Type local:Calculator}" MethodName="Add">
- <ObjectDataProvider.MethodParameters>
- <system:String>0</system:String>
- <system:String>0</system:String>
- </ObjectDataProvider.MethodParameters>
- </ObjectDataProvider>
- </Window.Resources>
- <StackPanel>
- <TextBox x:Name="textBox1" Margin="5" Text="{Binding Source={StaticResource odp}, Path=MethodParameters[0], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged}" />
- <TextBox x:Name="textBox2" Margin="5" Text="{Binding Source={StaticResource odp}, Path=MethodParameters[1], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged}"/>
- <TextBox x:Name="textBox3" Margin="5" Text="{Binding Source={StaticResource odp}, Mode=OneWay}"/>
- </StackPanel>
- </Window>
- <Window x:Class="BindingDemo.Window1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:BindingDemo"
- xmlns:system="clr-namespace:System;assembly=mscorlib"
- Title="Add" Height="300" Width="300">
- <Window.Resources>
- <ObjectDataProvider x:Key="odp" ObjectType="{x:Type local:Calculator}" MethodName="Add">
- <ObjectDataProvider.MethodParameters>
- <system:String>0</system:String>
- <system:String>0</system:String>
- </ObjectDataProvider.MethodParameters>
- </ObjectDataProvider>
- </Window.Resources><StackPanel>
- <TextBox x:Name="textBox1" Margin="5" Text="{Binding Source={StaticResource odp}, Path=MethodParameters[0], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged}" />
- <TextBox x:Name="textBox2" Margin="5" Text="{Binding Source={StaticResource odp}, Path=MethodParameters[1], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged}"/>
- <TextBox x:Name="textBox3" Margin="5" Text="{Binding Source={StaticResource odp}, Mode=OneWay}"/>
- </StackPanel></Window>
说明: 1.xaml 文件中对于命名空间的定义:
- xmlns:local="clr-namespace:BindingDemo"
- xmlns:system="clr-namespace:System;assembly=mscorlib"
我们应该知道在 xaml 文件中其实并没有引入. net 常规类库中命名空间, 如 System,System.Data 等, 如果我们需要在 xaml 文件中使用, 则需要将对应的命名空间添加到 xaml 中
2.<TextBox x:Name="textBox1" Margin="5" Text="{Binding Source={StaticResource odp}, Path=MethodParameters[0], BindsDirectlyToSource=true, UpdateSourceTrigger=PropertyChanged}" />
这里需要补充说明的是 UpdateSourceTrigger 属性, 它绑定了数据更新的规则. UpdateSourceTrigger 有一下四种取值:
Default------- 它是 UpdateSourceTrigger 的默认取值. 当 UpdateSourceTrigger 属性被设置为改枚举值的时候, 对数据的更新则会根据根据参与绑定的属性进行相应的更改.
PropertyChanged---- 只要数据源中有意思的更改, 数据绑定机制将自动那个刷新目标数据的值.
LostFocus---- 当绑定的目标失去输入焦点的时候, 数据绑定机制将自动刷新目标数据的值.
Explicit------ 只有再调用 BindingExpression 的 UpdateSource 函数的情况下, 目标数据的值才会被刷新.
来源: http://www.bubuko.com/infodetail-2614865.html