如何在MVVM框架下对gridview中的button绑定事件

来源:互联网 发布:mac两个窗口并排 编辑:程序博客网 时间:2024/04/30 18:10

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>


原创粉丝点击