您的位置:首页 > 其它

WPF 数据绑定[10]

2010-01-08 10:31 447 查看
由于WPF可以绑定任意的.Net对象,所以只要该对象符合一定的机制和规则就可以被绑定并呈现出来。

而出于便利的目的,和之前的vs版本一样,在数据绑定方面WPF也提供了相应的Provider【尽管不是专门为关系型数据库提供的=-=】

 

第一部分、XmlDataProvider

这是一个提供用于绑定XML的对象,配合数据岛的相关方法进行使用,将会使得绑定XML变得很方便

1、页面XAML的代码如下:

<Window x:Class="WpfApplication1.Window13_xmlData"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window13_xmlData" Height="300" Width="300">
<Window.Resources>
<XmlDataProvider x:Key="xmlExample">
<x:XData>
<Guid xmlns="">
<Product name="手机">
<sumCount>15</sumCount>
</Product>
<Product name="物料">
<sumCount>25</sumCount>
</Product>
<Product name="电脑">
</Product>
</Guid>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<Label Content="{Binding Source={StaticResource xmlExample},XPath=/Guid/Product/@name}"></Label>
<Label Content="{Binding Source={StaticResource xmlExample},XPath=/Guid/Product/@name,Path=OuterXml}"></Label>
<ListBox ItemsSource="{Binding Source={StaticResource xmlExample},XPath=/Guid/Product/@name}"></ListBox>
</StackPanel>
</Grid>
</Window>


A、和其他资源一样,对于一个XmlDataProvider必须要提供一个唯一键用以标识。

 

B、绑定的时候,使用的是XPath,但是同时也是可以使用Path,Path主要是用于绑定对象的属性的获取,也就是说,在代码中的xml资源实际上是一个XmlNode或者XmlNodeList,所以可以通过Path获得这些对象的相关属性。

 

C、对于一般的XPath语法在这里都适用。

 

2、运行结果如下:



 

另外,也可以使用自动以的XML文件作为资源,只要将该文件添加到项目中,并设置Source就可以了,例如:Source=“aa.xml”这样。

 

第二部分、ObjectDataProvider

虽然之前已经实现了对于自定义对象的数据绑定,但是在使用的时候,还是遇到一些限制,主要是来自于之前的绑定没有办法针对函数进行,也就是说:

A、在构造的时候没有办法调用非默认的构造函数

B、没有办法对于具有返回值的函数进行相关的绑定

 

为了解决这些问题,可以使用ObjectDataProvider,换言之,使用这个对象之后,可以在代码中选择使用初始化的构造函数(而不用在onload事件中更改属性)以及可以通过事件返回相应可绑定的东西。

 

1、数据源类的代码如下:

public class ObjectForBinding : INotifyPropertyChanged
{
private string no = string.Empty;
private string name = string.Empty;
private string address = string.Empty;

public ObjectForBinding()
{
this.no = "0";
this.name = "peter";
this.address = "add0";
}

public ObjectForBinding(string No, string name, string address)
{
this.no = No;
this.name = name;
this.address = address;
}

public ObjectForBinding(string No, string name)
{
this.no = No;
this.name = name;
}

public ObjectForBinding(string No)
{
this.no = No;
}

public String Address
{
get
{ return this.address; }
set
{
this.address = value;
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Address"));
}
}
}

public String Name
{
get
{ return this.name; }
set
{
this.name = value;
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
}

public String No
{
get
{ return this.no; }
set
{
this.no = value;
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("No"));
}
}
}

public string GetCodeAndName()
{
return this.name + "_" + this.no;
}

public string GetCodeAndName(string param)
{
return this.name + "_" + param + "_" + this.no;
}

#region INotifyPropertyChanged 成员

public event PropertyChangedEventHandler PropertyChanged;

#endregion
}


 

2、XAML的代码如下:

<Window x:Class="WpfApplication1.Window13"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:location="clr-namespace:WpfApplication1"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window13" Height="300" Width="300">
<Window.Resources>
<location:ObjectForBinding x:Key="obj1"></location:ObjectForBinding>
<ObjectDataProvider x:Key="objData1" ObjectType="{x:Type location:ObjectForBinding}"
MethodName="GetCodeAndName"  >
<ObjectDataProvider.ConstructorParameters>
<sys:String>Code1</sys:String>
<sys:String>Name1</sys:String>
</ObjectDataProvider.ConstructorParameters>
<ObjectDataProvider.MethodParameters>
<sys:String>我是从objData1插入的数据</sys:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<Label Content="{Binding Source={StaticResource objData1}}"></Label>
</Grid>
</Window>


 

其实过程比较简单:

A、先设置一个数据源对象,然后再Provider里面将Provider对象和数据源对象绑定,这里由于使用了反射,所以要指定Type。

B、如果要使用构造函数,在使用相关子节点<ObjectDataProvider.ConstructorParameters>

C、如果要使用自定义函数,在使用子节点<ObjectDataProvider.MethodParameters>,同时要声明MethodName属性。

 

在数据源类中,这次绑定的构造函数以及自定义函数都是具有两个或以上的重载,所以Provider是根据参数的类型+数量来决定使用哪一个。

 

最后,如果要使用自定义函数返回值的话,不需要声明Path,如果是使用构造函数的话,则需要声明,因为构造函数不返回任何东西。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  wpf string binding path xml class