我的窗体布局是这样的: 最外层是一个Canvas,第二层也是一个Canvas,第三层是一个DataGrid,
DataGrid数据绑定上了,Command也绑定了,但是事件响应不了,后面测试发现整个DataGrid
所有事件都没有响应,DataGrid是这样的
<DataGrid AutoGenerateColumns="False" Canvas.Top="30" Width="330" Margin="0" ItemsSource="{Binding SearchSongCollect}"
Cursor="Hand" d:IsLocked="False" CanUserAddRows="False" CanUserDeleteRows="False" GridLinesVisibility="None" FontSize="13"
IsHitTestVisible="True" MouseLeftButtonDown="DataGrid_MouseLeftButtonDown" Background="Blue" VerticalScrollBarVisibility="Auto">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#FFF1FAFF"/>
<Setter Property="FontSize" Value=" 20"/>
<Setter Property="Height" Value="30"/>
<Setter Property="Foreground" Value="Blue"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTemplateColumn SortMemberPath="IsSelected" Width="20">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Center" HorizontalAlignment="Center"
Command="{Binding Path=DataContext.SearchSongSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type DataGrid}}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="歌曲" Width="240" Binding="{Binding Song.SongName}"/>
<DataGridTextColumn Header="歌手" Width="60" Binding="{Binding Song.Singer}"/>
</DataGrid.Columns>
</DataGrid>
这要看你后台的命令怎么定义的了,绑定的地方能找到这个DataContext么?
可能是你后面没有绑定参数,Command="{Binding Path=DataContext.SearchSongSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type DataGrid}}}",CommandParameter="{Binding}"
如果还是不可以,看一下你的Datagrid绑定数据源有没有get;set;