<DataGrid ItemsSource="{Binding Occerences}"
AutoGenerateColumns="False"
IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Start index" Binding="{Binding Start}" />
<DataGridTextColumn Header="End index" />
<DataGridTextColumn Header="Length" />
<DataGridTextColumn Header="Pattern" />
<DataGridTemplateColumn Header="Sequence">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Sequence" Command="{Binding SequenceCommand}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Export">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Export" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button Content="Sequence" Command="{Binding SequenceCommand}" />
这不起作用。如何绑定到SequenceCommand(MyViewModel,ICommand SequenceCommand)?
我不能使用框架(mvvm light tool kit,prism等)。
答案 0 :(得分:4)
CellTemplate的DataTemplate
中的DataContext将更改为绑定到未找到SequenceCommand的当前项。您必须将RelativeSource设置为DataGrid
,并为Path
设置DataContext.SequenceCommand
,如下所示:
<Button Content="Sequence"
Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid},
Path=DataContext.SequenceCommand}" />