微笑的子龍

          for my dream
          posts - 1, comments - 0, trackbacks - 0, articles - 0

          Data Binding

          Posted on 2007-02-05 19:49 zilong 閱讀(186) 評(píng)論(0)  編輯  收藏
          參考文章: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ù)源。
          一般情況下,綁定源對(duì)象中多個(gè)屬性時(shí)用Data Context,綁定源對(duì)象中單個(gè)屬性時(shí)用Source。
          ?在綁定時(shí)?Source和Data Context效果一樣,只是Source的優(yōu)先級(jí)比Data Context?高。?如果綁定對(duì)象是一個(gè)xml,則{Binding}中屬性用xpath來指定應(yīng)該使用 XML 文檔中的哪個(gè)集合來填充。???
          2)ElementName:
          3)RelativeSource:
          2.{binding}
          ???bingding有source和path屬性,其中source屬性指定綁定的具體數(shù)據(jù)對(duì)象,path指定該對(duì)象的特定屬性。當(dāng)邏輯樹中有Data Context,可以不用設(shè)定binding中source屬性;當(dāng)binding中沒有設(shè)定path屬性表明綁定整個(gè)對(duì)象。
          <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>


          ?? 如果要將某一元素與對(duì)象的多個(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ù)對(duì)象綁定方式。?
          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ù)對(duì)象;
          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;

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 娄烦县| 南郑县| 商水县| 湾仔区| 鄯善县| 工布江达县| 长丰县| 洛扎县| 淅川县| 津南区| 从化市| 石嘴山市| 云林县| 页游| 鄂伦春自治旗| 澄江县| 定安县| 临朐县| 池州市| 洛扎县| 南平市| 永修县| 甘德县| 定边县| 滨海县| 日土县| 南丰县| 麟游县| 义乌市| 吉木萨尔县| 马龙县| 忻州市| 石阡县| 绥滨县| 大理市| 青龙| 昌图县| 开原市| 赣榆县| 宜兴市| 宁陵县|