前端页面:
<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);
}));

本文详细介绍了在WPF中如何使用样式和控制模板进行命令的绑定,并通过实例展示了如何将当前模型数据作为命令参数进行传递。通过绑定命令到按钮,实现了点击事件的响应,同时展示了RelayCommand的定义和使用。
2273

被折叠的 条评论
为什么被折叠?



