Binding使用的属性、DataContext上下文绑定必须使用的情况

RelativeSource属性 https://blog.csdn.net/yangwenxue1989/article/details/81624240

Binding时,如果明确知道数据源的Name,就能用Source或者ElementName进行绑定,但是有时候我们需要绑定的数据源可能没有明确的Name,此时我们就需要利用Binding的RelativeSource进行绑定,这种办法的意思是指当前元素和绑定源的位置关系。(其实就是绝对位置、相对位置)

 <!--CheckBox的IsChecked属性,绑定自定义的IsSelected属性,状态会被PropertyChanged触发改变-->
 <CheckBox IsChecked="{Binding Path=IsSelected,  UpdateSourceTrigger=PropertyChanged}"
           VerticalAlignment="Center" HorizontalAlignment="Center"
           Command="{Binding Path=DataContext.SelectMenuItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}" />

绑定的时候,有时候需要用DataContext.属性,原因参考http://www.bubuko.com/infodetail-1304649.html

 

posted @ 2019-08-16 17:58  夕西行  阅读(1230)  评论(0编辑  收藏  举报