微笑的子龍

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

          Data Binding

          Posted on 2007-02-05 19:49 zilong 閱讀(186) 評論(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;

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


          網站導航:
           
          主站蜘蛛池模板: 广南县| 保靖县| 读书| 班戈县| 龙陵县| 奉贤区| 巧家县| 萍乡市| 靖西县| 大渡口区| 东莞市| 莫力| 页游| 弥渡县| 宣恩县| 龙州县| 鄂州市| 临清市| 偃师市| 曲周县| 北碚区| 调兵山市| 金沙县| 新化县| 大化| 漳州市| 神木县| 嫩江县| 岑溪市| 商洛市| 江源县| 嘉祥县| 咸丰县| 思茅市| 庄浪县| 尚义县| 农安县| 左云县| 容城县| 香港| 葫芦岛市|