Dot-Net

如何將 WPF 綁定與 RelativeSource 結合使用?

  • September 17, 2008

如何使用RelativeSourceWPF 綁定以及有哪些不同的案例?

如果要綁定到對像上的另一個屬性:

{Binding Path=PathToProperty, RelativeSource={RelativeSource Self}}

如果您想獲得祖先的屬性:

{Binding Path=PathToProperty,
   RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}

如果您想在模板化父對像上獲取屬性(因此您可以在 ControlTemplate 中進行 2 路綁定)

{Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}}

或者,更短(這只適用於 OneWay 綁定):

{TemplateBinding Path=PathToProperty}

引用自:https://stackoverflow.com/questions/84278