WPF中Command和CommandParameter绑定写法


<Button Content="操作"
        Command="{Binding 命令名称}"
        CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=SelectedItem}" />
        

RelativeSource可以将某个控件作为参数转递给Command里面;
它有三个常用属性


		//RelativeSourceMode是一个枚举型
		//RelativeSourceMode.PreviousData表示允许在当前显示的数据项列表中绑定上一个数据项(不是包含数据项的控件)
		//RelativeSourceMode.TemplatedParent表示引用应用了模板的元素,其中此模板中存在数据绑定元素。
		//RelativeSourceMode.Self表示引用正在其上设置绑定的元素,并允许你将该元素的一个属性绑定到同一元素的其他属性上。
		//RelativeSourceMode.FindAncestor表示引用数据绑定元素的父链中的上级。
		public RelativeSourceMode Mode { get; set; }
		//获取或设置要查找的上级节点的类型。
        public Type AncestorType { get; set; }
 		//FindAncestor模式获取或设置要查找的上级级别。
        public int AncestorLevel { get; set; }
        

另个,在进行参数传递的时候,比如把自己传进去


CommandParameter="{Binding RelativeSource={RelativeSource Self}}"

你可能感兴趣的:(WPF)