微笑的子龍

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

          Data Binding

          Posted on 2007-02-05 19:49 zilong 閱讀(183) 評論(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允許元素從它的父元素繼承數據綁定的數據源。
          Source標記顯式指定數據源。
          一般情況下,綁定源對象中多個屬性時用Data Context,綁定源對象中單個屬性時用Source。
          ?在綁定時?Source和Data Context效果一樣,只是Source的優先級比Data Context?高。?如果綁定對象是一個xml,則{Binding}中屬性用xpath來指定應該使用 XML 文檔中的哪個集合來填充。???
          2)ElementName:
          3)RelativeSource:
          2.{binding}
          ???bingding有source和path屬性,其中source屬性指定綁定的具體數據對象,path指定該對象的特定屬性。當邏輯樹中有Data Context,可以不用設定binding中source屬性;當binding中沒有設定path屬性表明綁定整個對象。
          <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>


          ?? 如果要將某一元素與對象的多個屬性綁定時,
          ContentControl 不知道如何GreekGod 的信息。這時要用到DataTemplate,它的作用是指定數據顯示形式。
          ???<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屬性,這是因為自動地將Data Context設為數據對象綁定方式。?
          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有一個IEnumerable接口,是你想要顯示的items列表 Itemtemplate屬性指定用來控制數據顯示的datatemplate。
          c#:
          GreekGod greekGod = (GreekGod)(listBox.Items[0]);//綁定的數據對象;
          ListBoxItem lbi1 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(0));//返回ListBoxItem;
          ListBoxItem lbi2 = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromIndex(listBox.Items.CurrentItem));
          為了保證選中項與當前項同步,設定
          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;
          注意:
          調用ContainerFromIndex之前要先打開組合框。comboBox.IsDropDownOpen = true;

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 绥阳县| 岑巩县| 当阳市| 连城县| 上饶县| 凤城市| 绥江县| 甘德县| 多伦县| 昭觉县| 贵定县| 兴仁县| 丹寨县| 平塘县| 罗定市| 团风县| 平潭县| 绍兴市| 军事| 绵阳市| 黄冈市| 大名县| 涿州市| 府谷县| 宁陵县| 大同市| 苍溪县| 营口市| 习水县| 天祝| 德化县| 江油市| 班戈县| 闸北区| 崇左市| 秦皇岛市| 漯河市| 红原县| 炉霍县| 岑溪市| 眉山市|