天天看点

WPF学习笔记-DATAGRID的数据修改事件绑定1. DATAGRID

DATAGRID的数据修改事件绑定

  • 1. DATAGRID
    • 1.1 DATAGRID的数据修改事件如何实现命令绑定
      • 1.1.1 原来的处理方式
      • 1.1.2 使用MVVM绑定

1. DATAGRID

1.1 DATAGRID的数据修改事件如何实现命令绑定

将CellEditEnding由传统的实现方式修改为命令绑定方式:

1.1.1 原来的处理方式

  • xaml
<DataGrid
            Name="grid"
            AutoGenerateColumns="False"
            CanUserAddRows="False"
            ItemsSource="{Binding ParamsModel}"
            CellEditEnding="CellEditEnding" BeginningEdit="BeginningEdit" CanUserSortColumns="False"
            Background="#1D2532" Foreground="#EEEEEE" HeadersVisibility="All">
        </DataGrid>
           
  • 后台
private void BeginningEdit(object sender, System.Windows.Controls.DataGridBeginningEditEventArgs e)
        {
            if ((e.Row.Item as Item).Value != null)
            {
                editValue = Convert.ToDouble((e.Row.Item as Item).Value);
            }
            else  // 判断是否可以编辑,为NULL则禁止编辑
            {
                e.Cancel = true;
            }
        }

        private void CellEditEnding(object sender, System.Windows.Controls.DataGridCellEditEndingEventArgs e)
        {
            if (e.EditingElement as TextBox == null)
            {
                return;
            }

            if ((e.EditingElement as TextBox).Text != null)
            {
                Item changedItem = e.Row.Item as Item;
                double newValue = Convert.ToDouble((e.EditingElement as TextBox).Text.ToString());

                if (editValue != newValue)
                {
                    MessageBox.Show(string.Format(@"Name:{0}, New Value is {1}", changedItem.Name, newValue));
                }
            }
        }
           

1.1.2 使用MVVM绑定

  • 添加命名空间
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
           
  • XAML
<DataGrid
            x:Name="grid"
            AutoGenerateColumns="False"
            CanUserAddRows="False"
            ItemsSource="{Binding ParamsModel}"
            CanUserSortColumns="False"
            Background="#1D2532" Foreground="#EEEEEE" HeadersVisibility="All">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="CellEditEnding">
                    <prism:InvokeCommandAction Command="{Binding CellEditEndingCommand}" 
                                           CommandParameter="{Binding MyParameter}"/>
                    <prism:InvokeCommandAction Command="{Binding CellEditEndingCommand}"
                                               CommandParameter="{Binding MyParameter}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
       </DataGrid>
           
  • ViewModel
public DelegateCommand<object> BeginningEditCommand { get; set; }

        private void BeginningEditEvent(object datagridBeginningEditEventArgs)
        {
            if (datagridBeginningEditEventArgs != null && datagridBeginningEditEventArgs is DataGridBeginningEditEventArgs args)
            {
                if ((args.Row.Item as Item).Value != null)
                {
                    EditValue = Convert.ToDouble((args.Row.Item as Item).Value);
                }
                else
                {
                    args.Cancel = true;
                }
            }
        }

        public DelegateCommand<object> CellEditEndingCommand { get; set; }

        private void CellEditEndingEvent(object datagridCellEditEndingEventArgs)
        {
            if (datagridCellEditEndingEventArgs != null && datagridCellEditEndingEventArgs is DataGridCellEditEndingEventArgs args)
            {
                if ((args.EditingElement as TextBox).Text != null)
                {
                    Item changedItem = args.Row.Item as Item;
                    double newValue = Convert.ToDouble((args.EditingElement as TextBox).Text.ToString());

                    if (EditValue != newValue)
                    {
                        MessageBox.Show(string.Format(@"Name:{0}, New Value is {1}", changedItem.Name, newValue));
                    }
                }
            }
        }
		
        public MainWindowViewModel()
        {
            BuildTestData();

            ParamsModel = model.FlatModel;
			
			// 调用
            BeginningEditCommand = new DelegateCommand<object>(BeginningEditEvent);
            CellEditEndingCommand = new DelegateCommand<object>(CellEditEndingEvent);
        }
           

参考链接:Prism InvokeCommandAction使用参考