<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}" />
这是行不通的。如何绑定(bind)到SequenceCommand(MyViewModel,ICommand SequenceCommand)?
我不能使用框架(mvvm灯光工具套件,棱镜等)。
最佳答案
DataTemplate
中用于CellTemplate的DataContext将更改为绑定(bind)到未找到SequenceCommand的当前项。您必须将RelativeSource设置为DataGrid
,并为Path
设置DataContext.SequenceCommand
,如下所示:
<Button Content="Sequence"
Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid},
Path=DataContext.SequenceCommand}" />