How do I use WPF bindings with RelativeSource?

前端 未结 13 2423
执念已碎
执念已碎 2020-11-22 03:00

How do I use RelativeSource with WPF bindings and what are the different use-cases?

13条回答
  •  我寻月下人不归
    2020-11-22 03:32

    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 the property to the bound one

    b. TemplatedParent(value=1): This is used when defining the templates of any control and want to bind to a value/Property of the control.

    For example, define ControlTemplate:

      
            
     
    

    c. Self(value=2): When we want to bind from a self or a property of self.

    For example: Send checked state of checkbox as CommandParameter while setting the Command on CheckBox

    
    

    d. FindAncestor(value=3): When want to bind from a parent control in Visual Tree.

    For example: Bind a checkbox in records if a grid,if header checkbox is 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.

提交回复
热议问题