How do I use RelativeSource with WPF bindings and what are the different use-cases?
In WPF RelativeSource binding exposes three properties to set:
1. Mode: This is an enum that could have four values:
a. PreviousData(
value=0): It assigns the previous value of thepropertyto the bound oneb. TemplatedParent(
value=1): This is used when defining thetemplatesof any control and want to bind to a value/Property of thecontrol.For example, define
ControlTemplate:
c. Self(
value=2): When we want to bind from aselfor apropertyof self.For example: Send checked state of
checkboxasCommandParameterwhile setting theCommandonCheckBox
d. FindAncestor(
value=3): When want to bind from a parentcontrolinVisual Tree.For example: Bind a
checkboxinrecordsif agrid,ifheadercheckboxis checked
2. AncestorType: when mode is FindAncestor then define what type of ancestor
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type iDP:XamDataGrid}}
3. AncestorLevel: when mode is FindAncestor then what level of ancestor (if there are two same type of parent in visual tree)
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type iDP:XamDataGrid, AncestorLevel=1}}
Above are all use-cases for
RelativeSource binding.
Here is a reference link.