WPF中使用ObjectDataProvider绑定方法

  
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函数的情况下,目标数据的值才会被刷新。

posted @ 2010-04-27 12:56  正文  阅读(9211)  评论(3编辑  收藏  举报