WPF DataTomplate中Command无效
问题描述:在DataGrid 中DataTomplate中添加一个Button,Button添加Command,但是Command未生效。
问题原因:ItemTemplate的DataContext指代不明,需要改为父类的DataContext。
解决方法:使用RelativeSource关键字,手动指定DataComtext和Command。
<!--Command 调用无效-->
<Button Width="45"
Command="{Binding cmdShowDetialView}"
CommandParameter="{Binding ElementName=_dataGrid, Path=SelectedItem}"
Content="细表"
FontSize="10"
Foreground="#9933ff"
Style="{StaticResource ButtonBaseStyle}"
ToolTip="显示细表" />
<!--使用RelativeSource关键字,手动指定DataComtext和Command-->
<Button Width="45"
Command="{Binding DataContext.cmdShowDetialView, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"
CommandParameter="{Binding ElementName=_dataGrid, Path=SelectedItem}"
Content="细表"
FontSize="10"
Foreground="#9933ff"
Style="{StaticResource ButtonBaseStyle}"
ToolTip="显示细表" />

浙公网安备 33010602011771号