1. 程式人生 > >WPF Binding源

WPF Binding源

type relative alt find 屬性。 template sda get blog

Bingding的源:

有三個屬性用來設置源:ElementName(string)、Source(Object) 和 RelativeSource(RelativeSource)。註:這三個只能指定一個,否則異常。
ElementName: 源為一個元素(Element),這裏用的是此元素中設置的Name屬性。
Source:以object作為源。<TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}"/>
RelativeSource: 源相對於綁定目標的位置。
源是元素本身:{Binding RelativeSource={RelativeSource Self}}
源是Tempalte中元素的Parent:{Binding RelativeSource={RelativeSource TemplatedParent}}
源是綁定以collection形式的前一個數據:{Binding RelativeSource={RelativeSource PreviousData}},MSDN上關於PreviousData的說明並不多,這裏有一篇文章可以參考
以上三項為RelativeSource中的Static值,使用這些值可以減少內存開銷
源是Ancestor(可能比parent還高):{Binding RelativeSource={RelativeSource FindAncestor,
AncestorLevel=n, AncestorType={x:Type desiredType}}}

WPF Binding源