devexpress 的 grid 增加行删除功能

1、新建UserControl起名为    ContextMenuModule,示例代码如下:

<UserControl x:Class="Main.UserControls.EvidencePackageList"
             x:Name="ContextMenuModule"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Main.UserControls"
             xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
             xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
             xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
             xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
             xmlns:dxrudex="http://schemas.devexpress.com/winfx/2008/xaml/reports/userdesignerextensions"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800" Loaded="UserControl_Loaded">

2、建一个 grid  前台代码示例如下:

            <dxg:GridControl Name="grid" SelectedItemChanged="grid_SelectedItemChanged" MouseDoubleClick="grid_MouseDoubleClick" ContextMenu="{ StaticResource ContextMenu }" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <dxg:GridControl.View>
                    <dxg:TableView NavigationStyle="Row" x:Name="gridView" AllowColumnFiltering="False" AllowScrollAnimation="True" ShowGroupPanel="False" ShowGridMenu="GridView_ShowGridMenu">
                        <dxg:TableView.RowCellMenuCustomizations>
                            <dxb:BarButtonItem Name="refresh"
                                           Content="刷新"
                                           ItemClick="Refresh_Click"
                                           Command="{Binding ElementName=ContextMenuModule}"
                                           CommandParameter="{Binding ElementName=ContextMenuModule, Path=CellMenuInfo.Row.RowHandle.Value}" />
                            <dxb:BarButtonItem Name="deleteRow"
                                           Content="删除"
                                           Command="{Binding ElementName=ContextMenuModule,Path=DeleteRow}"
                                           CommandParameter="{Binding ElementName=ContextMenuModule, Path=CellMenuInfo.Row.RowHandle.Value}" />
                        </dxg:TableView.RowCellMenuCustomizations>
                    </dxg:TableView>
                </dxg:GridControl.View>

                <dxg:GridControl.Columns>
                    <dxg:GridColumn  Header="ID" HorizontalHeaderContentAlignment="Left" FixedWidth="True" FieldName="ID" Width="*" Visible="False" />
                    <dxg:GridColumn  Header="名称" HorizontalHeaderContentAlignment="Left" FixedWidth="True" FieldName="NAME" Width="*" />
                    </dxg:GridColumn>
                </dxg:GridControl.Columns>
            </dxg:GridControl>

3、后台加立事件

        public static readonly DependencyProperty CellMenuInfoProperty = DependencyPropertyManager.Register("CellMenuInfo", typeof(GridCellMenuInfo), typeof(EvidencePackageList), new FrameworkPropertyMetadata(null));
        public ICommand DeleteRow { get; private set; }
        public EvidencePackageList()
        {
            DeleteRow = new DelegateCommand<object>(OnDeleteRow);
            InitializeComponent();
        }

        public GridCellMenuInfo CellMenuInfo
        {
            get { return (GridCellMenuInfo)GetValue(CellMenuInfoProperty); }
            set { SetValue(CellMenuInfoProperty, value); }
        }

备注: 

Command="{Binding ElementName=ContextMenuModule,Path=DeleteRow}"   里的指的是当前用户控件的名称。

猜你喜欢

转载自www.cnblogs.com/wjx-blog/p/11834594.html