WPF指定數(shù)據(jù)源創(chuàng)建過程解析
WPF工具中有許多功能技巧需要我們在實(shí)際編程開發(fā)中去總結(jié),擁有一套自己熟悉的操作方法。在這里我們將會(huì)了解到有關(guān)WPF指定數(shù)據(jù)源的相關(guān)方法。#t#
建立一個(gè)數(shù)據(jù)源對象,供一個(gè)范圍使用(如一個(gè)容器)
此示例建立在第一個(gè)示例之上
WPF指定數(shù)據(jù)源1.創(chuàng)建對象(為Person添加一個(gè)屬性)
- public int Age { get; set; }
WPF指定數(shù)據(jù)源2.在Resources中聲明對象
- < DockPanel.Resources>
- < src:Person Age="22"
Name="clingingboy"
x:Key="xxx" >< /src:Person> - < 小/DockPanel.Resources>
WPF指定數(shù)據(jù)源3.創(chuàng)建一個(gè)數(shù)據(jù)源對象DataContext屬性,其綁定源的數(shù)據(jù)只提供其上下文使用(其容器范圍內(nèi))
- < DockPanel.DataContext>
- < Binding Source=
"{StaticResource xxx}"/>- < /DockPanel.DataContext>
WPF指定數(shù)據(jù)源4.從數(shù)據(jù)源獲取數(shù)據(jù)(直接指定Path屬性)
- < TextBox Width="100"
Text="{Binding Path=Name}"
Height="25" />- < TextBox Width="100"
Text="{Binding Path=Age}"
Height="25" />
注意此標(biāo)簽與一下標(biāo)簽是等價(jià)的
- < TextBox Width=
"100" Height="25">- < TextBox.Text>
- < Binding Path="Name"/>
- < /TextBox.Text>
- < /TextBox>
以上就是對WPF指定數(shù)據(jù)源的方法介紹。