微笑的子龙

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

2007年2月5日

参考文章: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;

posted @ 2007-02-05 19:49 zilong 阅读(181) | 评论 (0)编辑 收藏