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

时间:2023-03-09 07:53:13
在ListBoxItem的样式中的button传参,把当前选中项传递到命令的方法

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

前端页面:

<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);
}));