原文: 在ListBoxItem的样式中的button传参,把当前选中项传递到命令的方法

前端页面:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
<Style x:Key="ThumbItemStyle" TargetType="{x:Type ListBoxItem}">
	<Setter Property="Template">
		<Setter.Value>
			<ControlTemplate TargetType="{x:Type ListBoxItem}">
				<Grid>
					<Button 
						Command="{Binding DataContext.ThumbClickCommand, RelativeSource={RelativeSource AncestorType={x:Type local:StudentEvaluateView}, Mode=FindAncestor}}"
						CommandParameter="{Binding .}">
					</Button>
				</Grid>
			</ControlTemplate>
		</Setter.Value>
	</Setter>
</Style>

绑定时命令参数写{Binding .}即传递的是当前自己的模型数据

绑定命令写法:

private RelayCommand<object> thumbClick;
public RelayCommand<object> ThumbClickCommand => thumbClick ?? (thumbClick = new RelayCommand<object>(obj =>
{
	Debug.WriteLine(obj);
}));

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄