Binding的Source从何而来?
I. Binding to Object
1. Binding data using ObjectDataProvider
AC:Let’s say there is a CLR based data bound object that you are trying to implement. For example a collection of Tool objects, e.g.
- An Object called Tool that contains a bunch of properties that we want to bind (in this case it just contains a description)
- A Collection of Tool objects(ToolsCollection); for example to populate a list box etc
- A Factory to get the reference of ToolsCollection objects which have collection<Tools>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CLRDataBinding
{
public class Tool : INotifyPropertyChanged
{
private string _description = "";
public string Description
{
get
{
return _description;
}
set
{
if (_description != value)
{
_description = value;
NotifyPropertyChanged("Description");
}
}
}
public Tool(string description)
{
_description = description;
} public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
} }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel; namespace CLRDataBinding
{
public class ToolsCollection : ObservableCollection<Tool>
{
public ToolsCollection()
{
CreateToolsData();
} private void CreateToolsData()
{
for (int loop = ; loop < ; loop++)
{
this.Add(new Tool("Tool " + loop.ToString()));
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CLRDataBinding
{
public class Factory
{
public ToolsCollection MyToolsCollection
{
get { return new ToolsCollection(); }
}
}
}
<Window x:Class="CLRDataBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:clr="clr-namespace:CLRDataBinding"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<ObjectDataProvider x:Key="FactoryDP" ObjectType="{x:Type clr:Factory}" />
<DataTemplate x:Key="ToolItemTemplate">
<StackPanel>
<TextBlock Text="{Binding Description}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemTemplate="{StaticResource ToolItemTemplate}" ItemsSource="{Binding Path=MyToolsCollection, Source={StaticResource FactoryDP}}" />
</Grid>
</Window>
之前一篇文章 对象数据绑定 里提到三个例子,第一个例子用了ObjectDataProvider没有用DataContext,第二个例子用了DataContext没有用 ObjectDataProvider,第三个例子既用了ObjectDataProvider也用了DataContext,但没有提到它们的区别,正 好在Beatriz Costa的blog上看到一篇好文章解释了为什么需要 ObjectDataProvider的问题。
ObjectDataProvider能实现四个特殊功能:
1. 传递参数到构造函数中
使用下面的XAML语句定义一个ObjectDataProvider,它会自动调用MySource类的默认构造函数初始化类
<ObjectDataProvider ObjectType="{x:Type local:MySource}" x:Key="odp1"/>
如果MySource类的构造函数允许传入参数的话,就可以这样定义ObjectDataProvider:
<ObjectDataProvider ObjectType="{x:Type local:MySource}" x:Key="odp1">
<ObjectDataProvider.ConstructorParameters>
<system:String>Jupiter</system:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
2. 绑定到方法
ObjectDataProvider 除了 ObjectType的属性外还有MethodName的属性,MethodName属性将ObjectDataProvider绑定到方法,相当于是对数据源的包装,另外也可以定义方法的传入参数:
<ObjectDataProvider ObjectInstance="{StaticResource odp1}" MethodName="WeightOnPlanet" x:Key="odp2">
<ObjectDataProvider.MethodParameters>
<system:Double>95</system:Double>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
3. 替换数据对象
和使用自己在resource中定义对象不同,ObjectDataProvider可以使里很简单的更换绑定的数据对象,只需要更换一下绑定的数据对象的名字。而使用自己在resource中定义对象,即使定义了相同的x:Key也不能达到自动更新的目的。
另,这里使用DataContext也能达到与ObjectDataProvider同样的效果。
4. 建立异步的数据对象
ObjectDataProvider有IsAsynchronous 可以将数据对象定义为异步的。
默认情况下ObjectDataProvider是同步的,XmlDataProvider是异步。
2. Binding DataContext in Window.DataContext (ObjectDataProvider not needed)
AuctionItem.cs: define Class AuctionItem : INotifyPropertyChanged
MyApp.xaml.cs: define Class AuctionItems : ObservableCollection<AuctionItem>
Window1.xaml.cs: use [CollectionViewSource cv = root.DataContext as CollectionViewSource;] to action in button event handler
Binding Process:
1)Define DataContext in Window1.xaml:
<Window.DataContext>
<CollectionViewSource>
<CollectionViewSource.Source>
<local:AuctionItems/> </CollectionViewSource.Source>
</CollectionViewSource>
</Window.DataContext>
2) Binding Listbox:
<ListBox ItemsSource="{Binding Path=.}" IsSynchronizedWithCurrentItem="True">
3) Binding label: (the current item in listbox)
<Label Content="{Binding Path=/}" >
4) DataTemplate
<DataTemplate DataType="{x:Type local:AuctionItem}" >
...
<Image>
<Image.Source>
<Binding Path="Image"/>
</Image.Source>
</Image>
...
More: this sample also shows how to use Converter and MultiBinding
3. Binding DataContext in controls to Global ObjectDataProvider
sample:
MyApp.xaml:
<Application.Resources>
...
<ObjectDataProvider x:Key="Employees" ObjectType="{x:Type local:MSEmployeeCollection}"/>
</Application.Resources>
Window1.xaml:
<StackPanel Margin="10" DataContext="{StaticResource Employees}">
...
<Button Content="{Binding Path=[0]}" />
<ComboBox ItemsSource="{Binding}" SelectedIndex="0" />
</StackPanel>
1) DataContext another format : binding to StackPanel
2) Button bind to single item [0]
3) ComblBox bind to multi items, so no Path
II. Binding to XML
1. Use Source include .xml file to XmlDataProvider
<?xml version='1.0' encoding='utf-8'?>
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Margin="10">
<StackPanel.Resources>
<XmlDataProvider x:Key="Blog" Source="http://home.wangjianshuo.com/index.xml"/>
<DataTemplate x:Key="TitleTemplate">
<TextBlock Text="{Binding XPath=title}"/>
</DataTemplate>
</StackPanel.Resources>
<Label Content="{Binding Source={StaticResource Blog}, XPath=/rss/channel/title}" FontSize="24" FontWeight="Bold" />
<Label Content="{Binding Source={StaticResource Blog}, XPath=/rss/channel/description}" FontSize="18" />
<DockPanel DataContext="{Binding Source={StaticResource Blog}, XPath=/rss/channel/item}" >
<ListBox DockPanel.Dock="Left" ItemsSource="{Binding}" ItemTemplate="{StaticResource TitleTemplate}" IsSynchronizedWithCurrentItem="True" />
<TextBox Name="Contents" Text="{Binding XPath=description}" TextWrapping="Wrap" Width="Auto" />
</DockPanel>
</StackPanel>
2. Use x:XData define the xml structure inside current XmlDataProvider
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel.Resources>
<XmlDataProvider x:Key="FavoriteColors">
<x:XData>
<Colors xmlns="">
<Color>Blue</Color>
<Color>Black</Color>
<Color>Green</Color>
<Color>Red</Color>
</Colors>
</x:XData>
</XmlDataProvider>
</StackPanel.Resources>
<TextBlock HorizontalAlignment="Center"
FontWeight="Bold">
XML Example
</TextBlock>
<ListBox Width="200" Height="300"
ItemsSource="{Binding Source={StaticResource FavoriteColors},
XPath=/Colors/Color}">
</ListBox>
</StackPanel>
III. Binding to Control
1:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Canvas>
<TextBox Name="theTextBox" Text="Hello" />
<TextBlock Canvas.Top="25">
<TextBlock.Text>
<Binding ElementName="theTextBox" Path="Text" />
</TextBlock.Text>
</TextBlock>
</Canvas>
</Window>
2:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Canvas>
<TextBox Name="theTextBox" Text="Hello" />
<TextBlock Canvas.Top="25"
Text="{Binding ElementName=theTextBox, Path=Text}" />
</Canvas>
</Window>
IX. Binding to ADO.NET
Similar to binding to object, create a dataset and return ds to DataContext.
Binding的Source从何而来?的更多相关文章
- 数据绑定(七)使用ObjectDataProvider对象作为Binding的Source
原文:数据绑定(七)使用ObjectDataProvider对象作为Binding的Source ObjectDataProvider就是把对象作为数据源提供给Binding,类似的还有XmlData ...
- 数据绑定(四)使用DataContext作为Binding的Source
原文:数据绑定(四)使用DataContext作为Binding的Source DataContext属性被定义在FrameworkElement类里,这个类是WPF控件的基类,这意味着所有WPF控件 ...
- 数据绑定(六)使用XML数据作为Binding的Source
原文:数据绑定(六)使用XML数据作为Binding的Source .NET Framework提供了两套处理XML数据的类库 1. 符合DOM标准的类库:包括XmlDocument.XmlEleme ...
- WPF Image Binding Uri Source 失败解决办法
在ListView 的ListItem里动态绑定Image. 首先代码写的是没有问题的.但最后运行却无法显示图片.先看代码: 1. XAML部分 代码如下: <ListView x:Name=& ...
- Binding 中 Elementname,Source,RelativeSource 三种绑定的方式
在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到过的问题做下汇总记录和理解. 1. so ...
- 绑定 Binding Path=.,Binding.,Binding Source={StaticResource ResourceKey="Hello"} xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:sys="clr-namespace:System;assembly=mscorlib" <Window.Resources> <Style Targ ...
- Binding笔记
Binding基础 绑定某个对象的属性值到控制上,写法如下: public class Order : INotifyPropertyChanged//只要实现此接口 { public event ...
- WPF之Binding初探
初学wpf,经常被Binding搞晕,以下记录写Binding的基础. 首先,盗用张图.这图形象的说明了Binding的机理. 对于Binding,意思是数据绑定,基本用法是: 1.在xmal中使用 ...
- Binding
Binding基础 绑定某个对象的属性值到控制上,写法如下: public class Order : INotifyPropertyChanged//只要实现此接口 { public event ...
随机推荐
- 学习java之HashMap和TreeMap
HashMap和TreeMap是Map接口的两种实现,ArrayDeque和LinkedList是Queue接口的两种实现方式.下面的代码是我今天学习这四个数据结构之后写的.还是不熟悉,TreeMap ...
- 每天一个Linux命令(7): cp
cp命令 该命令的功能是将给出的文件或目录拷贝到另一文件或目录中,同MSDOS下的copy命令一样,功能十分强大. 语法: cp [选项] 源文件或目录 目标文件或目录 ...
- SAS9.4 安装注意事项
OS:Windows Server 2012 R2 SAS:9.4 TS1M1 SAS9.4的安装需要注意的地方: 一. 注意开启Shortname(装了N遍,最容易忘记的地方) SAS安装使用Sho ...
- 【转】MIPS交叉编译环境的建立
原文网址:http://imgtec.eetrend.com/forum/2371 我觉得对于MIPS处理起来说最令新手头疼的应该就是编译环境的建立了,这点MIPS做的确实不是很好,不像ARM那样有许 ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.1创建虚拟机
2.1.创建虚拟机 2.1.1. 创建虚拟机节点1 2.1.2. 创建虚拟机节点2 操作如节点1. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境所有链 ...
- 跨站脚本攻击(Cross‐Site Scripting (XSS))实践
作者发现博客园在首页显示摘要时未做html标签的过滤,致使摘要中的html代码可以被执行,从而可以注入任何想要被执行的js代码,作者利用这一缺陷在本文摘要中插入了一段js代码执行alert弹窗,同时增 ...
- phpcms V9实现QQ登陆OAuth2.0
phpcmsV9使用的QQ登陆依然是OAuth1.0,但现在腾讯已经不审核使用OAuth1.0的网站了.这对于使用pc的站长来讲是一个无比巨大的坑.经过对phpcms论坛的一位同学做的插件进行修改,现 ...
- python GUI初步
- 第三百四十天 how can I 坚持
感觉还是要制定个计划,做不做不到是一回事,但是得制定.目标,一年时间进小米,加油,fordream 计划好好想想,技不在多,精就好. 晚上写了写杨辉三角,都不记得什么是杨辉三角了. 人言落日是天涯,望 ...
- 第二百三十四天 how can I 坚持
今天果然不负众望,下了一天的雪啊,挺好. 今天把花搞了下,都弄花盆里了,希望不会就这么挂掉.八千代,绿萝,还有小叶元宝. 中午喝了点酒,没感觉. 过两天气温就零下十多度了,该咋办啊,最怕冬天.家里现在 ...