<DataGrid Grid.ColumnSpan="" Grid.Column="" Grid.Row="" Grid.RowSpan="" Height="Auto"
SelectedItem="{Binding Path=SelectProductModel,Mode=TwoWay}"
ItemsSource="{Binding Path=ArriveProductModels,Mode=TwoWay}"
Cursor="Hand" HeadersVisibility="Column" AutoGenerateColumns="False" CanUserAddRows="False" SelectionUnit="FullRow" >
<DataGrid.Columns>
<DataGridTextColumn Header="产品类别" Width="" Binding="{Binding Path=product_catalog_id,Mode=OneWay, Converter={StaticResource CatalogIdToNameCvter}}"/>
<DataGridTextColumn Header="进仓票号" Width="" Binding="{Binding Path=into_warehouse_no,Mode=OneWay}"/>
<DataGridTemplateColumn Header="操作" Width="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="移除"
Command="{Binding DataContext.RemoveProductCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
当前项目中,绑定一个ViewModel,
RemoveProductCommand为ViewModel中的属性,
ArriveProductModels也为ViewModel中的属性,这样就造成command绑定无效,因为找不到,这个时候需要
RelativeSource
Command="{Binding DataContext.RemoveProductCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}}"
这样就可以绑定成功了,同样,将Button改为Combox,那么Combox的ItemSource
ItemSource={Binding DataContext.xxxx(数据源集合)
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}
}