WPF Databinding----Collections
来源:百度文库 编辑:神马文学网 时间:2024/04/29 02:28:51
WPF(Windows Presentation Fundation) databinding功能能很好的显示listbox, listview中的内容.在其中有四个要素:1.绑定目标(binding target), 2. 目标属性(target property), 3.绑定的源数据( binding source), 4. 绑定的属性,通过关键字Path来指定( a path to the source to use)如下图:
数据绑定几个必要做的事:
1. 实现 INotifyPorpertyChanged接口,
2.有属性改变时通过PropertyChangedEventHandler来捕捉
3.ObservableCollection是INotifyPropertyChanged内建的实现数据绑定.
4.XAML文件中必需添加xmlns:="clr-namespace:YourNameSpace"
好,让我们来看一个具体的例子.创建一个WPF应用程序,采用C#来做.
文件 Data.cs:
using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace Collections
{
public class Person : INotifyPropertyChanged
{
//the person‘s attribute sex
private string name;
private string sex;
public event PropertyChangedEventHandler PropertyChanged;
//person constructor
public Person()
{
}
public Person(string named, string sexy)
{
this.name = named;
this.sex = sexy;
}
//person name attribute
public string Name
{
get { return name; }
set
{
this.name = ;
OnPropertyChanged("Name");
}
}
//person sex attribute
public string Sex
{
get { return sex; }
set
{
this.sex = ;
OnPropertyChanged("Sex");
}
}
//override the ToString method for dispaly Person‘s name
// if not override it, it will be displayed as "Collections.Person" but not person‘s name
public override string ToString()
{
return name.ToString();
}
//cacth up the property changed event
protected void OnPropertyChanged(string inputstring)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this,new PropertyChangedEventArgs(inputstring));
}
}
}
public class People : ObservableCollection
{
public People():base()
{
Add(new Person("Ekin", "Male"));
Add(new Person("Jack", "Male"));
Add(new Person("Jane", "Female"));
}
}
}
Windows1.xaml文件,注意XAML文件必需保持父子层严格的缩进.
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Local="clr-namespace:Collections"
Title="Window1" Height="300" Width="300">
BorderBrush="Red" BorderThickness="1" Padding="8">
ContentTemplate="{Binding Source = {StaticResourceDetailTemplate}}" />
在这里相对的绑定我都用相同的着色标示出来了.
OK,这就搞定了.
数据绑定几个必要做的事:
1. 实现 INotifyPorpertyChanged接口,
2.有属性改变时通过PropertyChangedEventHandler来捕捉
3.ObservableCollection是INotifyPropertyChanged内建的实现数据绑定.
4.XAML文件中必需添加xmlns:
好,让我们来看一个具体的例子.创建一个WPF应用程序,采用C#来做.
文件 Data.cs:
using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace Collections
{
public class Person : INotifyPropertyChanged
{
//the person‘s attribute sex
private string name;
private string sex;
public event PropertyChangedEventHandler PropertyChanged;
//person constructor
public Person()
{
}
public Person(string named, string sexy)
{
this.name = named;
this.sex = sexy;
}
//person name attribute
public string Name
{
get { return name; }
set
{
this.name = ;
OnPropertyChanged("Name");
}
}
//person sex attribute
public string Sex
{
get { return sex; }
set
{
this.sex = ;
OnPropertyChanged("Sex");
}
}
//override the ToString method for dispaly Person‘s name
// if not override it, it will be displayed as "Collections.Person" but not person‘s name
public override string ToString()
{
return name.ToString();
}
//cacth up the property changed event
protected void OnPropertyChanged(string inputstring)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this,new PropertyChangedEventArgs(inputstring));
}
}
}
public class People : ObservableCollection
{
public People():base()
{
Add(new Person("Ekin", "Male"));
Add(new Person("Jack", "Male"));
Add(new Person("Jane", "Female"));
}
}
}
Windows1.xaml文件,注意XAML文件必需保持父子层严格的缩进.
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Local="clr-namespace:Collections"
Title="Window1" Height="300" Width="300">
在这里相对的绑定我都用相同的着色标示出来了.
OK,这就搞定了.
WPF Databinding----Collections
WPF, WPF/E释疑
WPF概述
用 ListBox 和 DataBinding 显示列表数据
Initializing collections and proxies
art photo collections (kjkimberly)
scenery collections 27(kjkimberly)
scenery collections 26(kjkimberly)
scenery collections 25(kjkimberly)
scenery collections 24(kjkimberly)
scenery collections 23(kjkimberly)
scenery collections 22(kjkimberly)
scenery collections 21(kjkimberly)
flower photos collections
Perseus Collections/Texts
WPF中的传递事件
WPF性能优化点
WPF开发简介
WPF 路径标记语法
WPF中的动画.
对比MFC,winform,wpf
WPF分辨率的独立性
sports photo collections 4(kjkimberly)
sports photo collections 3(kjkimberly)