0.假设Viewmodel中有一个名为Save的Commad。
1. 定义类DataContextProxy.cs
public class DataContextProxy : DependencyObject
{
public static readonly DependencyProperty DataContextProperty =
DependencyProperty.Register(
"DataContext",
typeof(object),
typeof(DataContextProxy),
new PropertyMetadata(null));
public object DataContext
{
get { return GetValue(DataContextProperty); }
set { SetValue(DataContextProperty, value); }
}
}
2.在页面上定义资源
<UserControl.Resources>
<NodeModel:DataContextProxy x:Key="dataContextProxy" DataContext="{Binding Object}"/>
</UserControl.Resources>
3.在gridview中绑定
<telerik:GridViewColumn>
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<telerik:RadButton Content="Delete" Command = "{Binding Path=Save}" DataContext="{Binding Source={StaticResource dataContextProxy}, Path=DataContext}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}">
</telerik:RadButton>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>