綁定相關知識點說明

來源:互聯網
上載者:User
一、綁定要點

一個標準的綁定三要素:繫結來源、綁定方式、繫結目標。

例:<TextBox x:Name="MyTextBox" Text="Text" Foreground="{Binding Brush1, Mode=OneWay}"/>

TextBox.Foreground屬性為繫結目標

Brush1為繫結來源

花括弧之間內容為綁定方式

只有相依性屬性才可以做為繫結目標

1、繫結來源

在SilverLight中存在三種不同的繫結來源:一般綁定、元素繫結、位置綁定

一般綁定:【Source】  任何對象的引用  <TextBlock x:Name="TextBlock1" Text="test" Foreground="{Binding Path=color1, Source=vm}" />

元素繫結:【ElementName】  同一個XAML中的元素,以Name或x:Name關聯  <TextBlock Text="{Binding ElementName=TextBlock1, Path=Text}" />

位置綁定:【RelativeSource】  相對於繫結目標的位置來指定資料來源

有一個比較特殊的綁定叫模板內綁定【TemplateBinding】,某一個元素的模板內部,可以直接綁定到這個元素上的屬性。在資料範本中特別有用。

<Button Content="Click">  <Button.Template>    <ControlTemplate>      <TextBlock Text="{TemplateBinding Content}" />    </ControlTemplate>  </Button.Template></Button>2、綁定方式

綁定的書寫方式非常靈活,初學容易被混淆。

標準寫法:  {Binding Path=PropertyName , Source=Object ……}  明確指定繫結來源與綁定屬性

變形一:    {Binding Path=PropertyName  ……}              只指定了綁定屬性,省略繫結來源時,以當前上下文為繫結來源

變形二:    {Binding PropertyName  ……}                   省略”Path“標記

變形三:    {Binding Source=Object ……}                   只指定繫結來源,綁定直接綁定到目標上。

也有一種特殊的綁定方式即資源綁定

這種方式可以直接將繫結來源指向資源字典的某一項,比較常用,下面種寫法不同,但效果相同

<Button Style="{StaticResource ButtonStyle}" /><Button Style="{Binding Source={StaticResource ButtonStyle}}" />3、繫結目標

繫結目標必須是依賴項屬性

二、綁定標記延伸

綁定的標記延伸中有關鍵的幾個需要牢記:Path、Source、ElementName、RelativeSource、Mode

其中Source、ElementName、RelativeSource是指定繫結來源、Mode指定繫結模式、Path是指定繫結來源的具體屬性

在Path中可以使用點文法指定Path=PropertyName.PropertyName2也可以使用索引指定(無引號)Path=PropertyList[0]Path=PropertyList[Key]也可以混合使用Path=PropertyName.PropertyList[0]Path=PropertyList[Key].PropertyName

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.