參考文章:http://www.beacosta.com/Archive/2005_09_01_bcosta_archive.html?
??1.綁定方式:
?????? 1)Source/Data Context:
????????????<Window.Resources>
??????????<local:GreekGod Name="Zeus" Description="Supreme God of????the????Olympians"???????RomanName="Jupiter" x:Key="zeus"/>
????<local:GreekGod Name="Poseidon" Description="God of the sea, earthquakes and horses"????RomanName="Neptune" x:Key="poseidon"/>
</Window.Resources>
<StackPanel DataContext="{StaticResource poseidon}">
????<TextBlock TextContent="{Binding Source={StaticResource zeus}, Path=Name}"/>
????<TextBlock TextContent="{Binding Path=Description}"/>
????<TextBlock TextContent="{Binding Path=RomanName}"/>
</StackPanel>
Data Context允許元素從它的父元素繼承數(shù)據(jù)綁定的數(shù)據(jù)源。
Source標(biāo)記顯式指定數(shù)據(jù)源。
一般情況下,綁定源對象中多個(gè)屬性時(shí)用Data Context,綁定源對象中單個(gè)屬性時(shí)用Source。?在綁定時(shí)?Source和Data Context效果一樣,只是Source的優(yōu)先級比Data Context?高。?如果綁定對象是一個(gè)xml,則{Binding}中屬性用xpath來指定應(yīng)該使用 XML 文檔中的哪個(gè)集合來填充。???
2)ElementName:
3)RelativeSource:
2.{binding}
???bingding有source和path屬性,其中source屬性指定綁定的具體數(shù)據(jù)對象,path指定該對象的特定屬性。當(dāng)邏輯樹中有Data Context,可以不用設(shè)定binding中source屬性;當(dāng)binding中沒有設(shè)定path屬性表明綁定整個(gè)對象。
<Window.Resources>
????<local:GreekGod Name="Zeus" Description="Supreme God of the Olympians" RomanName="Jupiter" x:Key="zeus"/>
</Window.Resources>
<Border DataContext="{StaticResource zeus}">
????<ContentControl Content="{Binding}"/>
</Border>
?? 如果要將某一元素與對象的多個(gè)屬性綁定時(shí),ContentControl 不知道如何GreekGod 的信息。這時(shí)要用到DataTemplate,它的作用是指定數(shù)據(jù)顯示形式。
???<Window.Resources>
????<local:GreekGod Name="Zeus" Description="Supreme God of the Olympians" RomanName="Jupiter" x:Key="zeus"/>
????<DataTemplate x:Key="contentTemplate">
????????<DockPanel>
????????????<TextBlock Foreground="RoyalBlue" TextContent="{Binding Path=Name}" />
????????????<TextBlock TextContent=":" Margin="0,0,5,0" />
????????????<TextBlock Foreground="Silver" TextContent="{Binding Path=Description}" />
????????</DockPanel>
????</DataTemplate>
</Window.Resources>
<Border DataContext="{StaticResource zeus}">
????<ContentControl Content="{Binding}" ContentTemplate="{StaticResource contentTemplate}"/>
</Border>
注意:DataTemplate中的{binding}沒有source屬性,這是因?yàn)樽詣?dòng)地將Data Context設(shè)為數(shù)據(jù)對象綁定方式。?
3、get ListItem from a data bound Listbox
xaml:
??? <Window.Resources>
????<local:GreekGods x:Key="greekGods"/>
????<DataTemplate x:Key="itemTemplate">
????????<TextBlock Text="{Binding Path=Name}" />
????</DataTemplate>
</Window.Resources>
<ListBox ItemsSource="{StaticResource greekGods}" ItemTemplate="{StaticResource itemTemplate}" Name="listBox"/>
listBox的ItemSource有一個(gè)IEnumerable接口,是你想要顯示的items列表 Itemtemplate屬性指定用來控制數(shù)據(jù)顯示的datatemplate。
c#:
GreekGod greekGod = (GreekGod)(listBox.Items[0]);//綁定的數(shù)據(jù)對象;
ListBoxItem lbi1 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(0));//返回ListBoxItem;
ListBoxItem lbi2 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(listBox.Items.CurrentItem));
為了保證選中項(xiàng)與當(dāng)前項(xiàng)同步,設(shè)定 IsSynchronizedWithCurrentItem= "true".
4.get a ComboBoxItem from a data bound ComboBox
與listbox相似;
Window.Resources>
????<local:GreekGods x:Key="greekGods"/>
????<DataTemplate x:Key="itemTemplate">
????????<TextBlock Text="{Binding Path=Name}" />
????</DataTemplate>
</Window.Resources>
<ComboBox ItemsSource="{StaticResource greekGods}" ItemTemplate="{StaticResource itemTemplate}" Width="200" Name="comboBox"/>
c#:
GreekGod greekGod = (GreekGod)(comboBox.Items[0]);
comboBox.IsDropDownOpen = true;
ComboBoxItem cbi1 = (ComboBoxItem)(comboBox.ItemContainerGenerator.ContainerFromIndex(0));
ComboBoxItem cbi2 = (ComboBoxItem)(comboBox.ItemContainerGenerator.ContainerFromItem(comboBox.Items.CurrentItem));
comboBox.IsDropDownOpen = false;
注意:
調(diào)用ContainerFromIndex之前要先打開組合框。comboBox.IsDropDownOpen = true;
??1.綁定方式:
?????? 1)Source/Data Context:
????????????<Window.Resources>
??????????<local:GreekGod Name="Zeus" Description="Supreme God of????the????Olympians"???????RomanName="Jupiter" x:Key="zeus"/>
????<local:GreekGod Name="Poseidon" Description="God of the sea, earthquakes and horses"????RomanName="Neptune" x:Key="poseidon"/>
</Window.Resources>
<StackPanel DataContext="{StaticResource poseidon}">
????<TextBlock TextContent="{Binding Source={StaticResource zeus}, Path=Name}"/>
????<TextBlock TextContent="{Binding Path=Description}"/>
????<TextBlock TextContent="{Binding Path=RomanName}"/>
</StackPanel>
Data Context允許元素從它的父元素繼承數(shù)據(jù)綁定的數(shù)據(jù)源。
Source標(biāo)記顯式指定數(shù)據(jù)源。
一般情況下,綁定源對象中多個(gè)屬性時(shí)用Data Context,綁定源對象中單個(gè)屬性時(shí)用Source。?在綁定時(shí)?Source和Data Context效果一樣,只是Source的優(yōu)先級比Data Context?高。?如果綁定對象是一個(gè)xml,則{Binding}中屬性用xpath來指定應(yīng)該使用 XML 文檔中的哪個(gè)集合來填充。???
2)ElementName:
3)RelativeSource:
2.{binding}
???bingding有source和path屬性,其中source屬性指定綁定的具體數(shù)據(jù)對象,path指定該對象的特定屬性。當(dāng)邏輯樹中有Data Context,可以不用設(shè)定binding中source屬性;當(dāng)binding中沒有設(shè)定path屬性表明綁定整個(gè)對象。
<Window.Resources>
????<local:GreekGod Name="Zeus" Description="Supreme God of the Olympians" RomanName="Jupiter" x:Key="zeus"/>
</Window.Resources>
<Border DataContext="{StaticResource zeus}">
????<ContentControl Content="{Binding}"/>
</Border>
?? 如果要將某一元素與對象的多個(gè)屬性綁定時(shí),ContentControl 不知道如何GreekGod 的信息。這時(shí)要用到DataTemplate,它的作用是指定數(shù)據(jù)顯示形式。
???<Window.Resources>
????<local:GreekGod Name="Zeus" Description="Supreme God of the Olympians" RomanName="Jupiter" x:Key="zeus"/>
????<DataTemplate x:Key="contentTemplate">
????????<DockPanel>
????????????<TextBlock Foreground="RoyalBlue" TextContent="{Binding Path=Name}" />
????????????<TextBlock TextContent=":" Margin="0,0,5,0" />
????????????<TextBlock Foreground="Silver" TextContent="{Binding Path=Description}" />
????????</DockPanel>
????</DataTemplate>
</Window.Resources>
<Border DataContext="{StaticResource zeus}">
????<ContentControl Content="{Binding}" ContentTemplate="{StaticResource contentTemplate}"/>
</Border>
注意:DataTemplate中的{binding}沒有source屬性,這是因?yàn)樽詣?dòng)地將Data Context設(shè)為數(shù)據(jù)對象綁定方式。?
3、get ListItem from a data bound Listbox
xaml:
??? <Window.Resources>
????<local:GreekGods x:Key="greekGods"/>
????<DataTemplate x:Key="itemTemplate">
????????<TextBlock Text="{Binding Path=Name}" />
????</DataTemplate>
</Window.Resources>
<ListBox ItemsSource="{StaticResource greekGods}" ItemTemplate="{StaticResource itemTemplate}" Name="listBox"/>
listBox的ItemSource有一個(gè)IEnumerable接口,是你想要顯示的items列表 Itemtemplate屬性指定用來控制數(shù)據(jù)顯示的datatemplate。
c#:
GreekGod greekGod = (GreekGod)(listBox.Items[0]);//綁定的數(shù)據(jù)對象;
ListBoxItem lbi1 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(0));//返回ListBoxItem;
ListBoxItem lbi2 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(listBox.Items.CurrentItem));
為了保證選中項(xiàng)與當(dāng)前項(xiàng)同步,設(shè)定 IsSynchronizedWithCurrentItem= "true".
4.get a ComboBoxItem from a data bound ComboBox
與listbox相似;
Window.Resources>
????<local:GreekGods x:Key="greekGods"/>
????<DataTemplate x:Key="itemTemplate">
????????<TextBlock Text="{Binding Path=Name}" />
????</DataTemplate>
</Window.Resources>
<ComboBox ItemsSource="{StaticResource greekGods}" ItemTemplate="{StaticResource itemTemplate}" Width="200" Name="comboBox"/>
c#:
GreekGod greekGod = (GreekGod)(comboBox.Items[0]);
comboBox.IsDropDownOpen = true;
ComboBoxItem cbi1 = (ComboBoxItem)(comboBox.ItemContainerGenerator.ContainerFromIndex(0));
ComboBoxItem cbi2 = (ComboBoxItem)(comboBox.ItemContainerGenerator.ContainerFromItem(comboBox.Items.CurrentItem));
comboBox.IsDropDownOpen = false;
注意:
調(diào)用ContainerFromIndex之前要先打開組合框。comboBox.IsDropDownOpen = true;
posted @ 2007-02-05 19:49 zilong 閱讀(183) | 評論 (0) | 編輯 收藏