【问题标题】:datagrid validation cell数据网格验证单元
【发布时间】:2012-08-14 11:21:57
【问题描述】:

我有一个绑定到 ObservableCollection 的数据网格,我想在 CellEditEnding 操作之前验证单元格输入。

例子:

如果验证返回 true -> CellEditEnding 操作; else -> 不要等待另一个输入;

这怎么可能?

我的班级:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using System.Text.RegularExpressions;


namespace WpfApplication7
{
    public partial class ProdList : INotifyPropertyChanged, IDataErrorInfo
    {
        private String mprodID = String.Empty;
        private String mprodName = String.Empty;
        private Double mprodPrice = 0;
        private Double mprodQuantity = 0;
        private String mprodSize = String.Empty;
        private String mprodColor = String.Empty;
        private Double mprodSubtotal = 0;
        private Double mprodDiscount = 0;


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        // This is the public factory method.
        public static ProdList CreateNewProdList()
        {
            return new ProdList();
        }

        public String prodID
        {
            get { return mprodID; }
            set
            {
                if (mprodID != value)
                {
                    mprodID = value;
                    NotifyPropertyChanged("prodID");
                }
            }
        }

        public String prodName
        {
            get { return mprodName; }
            set
            {
                if (mprodName != value)
                {
                    mprodName = value;
                    NotifyPropertyChanged("prodName");
                }
            }
        }

        public Double prodPrice
        {
            get { return mprodPrice; }
            set
            {

                if (mprodPrice != value)
                {
                    mprodPrice = value;
                    NotifyPropertyChanged("prodPrice");
                }
            }
        }

        public Double prodQuantity
        {
            get { return mprodQuantity; }
            set
            {
                if (mprodQuantity != value)
                {
                    mprodQuantity = value;
                    NotifyPropertyChanged("prodQuantity");

                }
            }
        }

        public String prodSize
        {
            get { return mprodSize; }
            set
            {
                if (mprodSize != value)
                {
                    mprodSize = value;
                    NotifyPropertyChanged("prodSize");
                }
            }
        }

        public String prodColor
        {
            get { return mprodColor; }
            set
            {
                if (mprodColor != value)
                {
                    mprodColor = value;
                    NotifyPropertyChanged("prodColor");
                }
            }
        }

        public Double prodSubtotal
        {
            get { return mprodSubtotal; }
            set
            {
                if (mprodSubtotal != value)
                {
                    mprodSubtotal = value;
                    NotifyPropertyChanged("prodSubtotal");
                }
            }
        }

        public Double prodDiscount
        {
            get { return mprodDiscount; }
            set
            {
                if (mprodDiscount != value)
                {
                    mprodDiscount = value;
                    NotifyPropertyChanged("prodDiscount");
                }
            }
        }


        #region IDataErrorInfo Members

        public string Error
        {
            get { return "Error occurred"; }
        }

        public string this[string columnName]
        {
            get
            {
                return this.GetResult(columnName);
            }
        }

        private string GetResult(string columnName)
        {
            Console.WriteLine("columnName: " + columnName);
            if (columnName.Equals("prodID"))
            {
                PropertyInfo info = this.GetType().GetProperty(columnName);
                if (info != null)
                {
                    string value = info.GetValue(this, null) as string;

                    if (string.IsNullOrEmpty(value))
                        return string.Format("{0} has to be set", info.Name);
                    else if (value.Length < 5)
                        return string.Format("{0}'s length has to be at least 5 characters !", info.Name);
                }

            }

            return null;

        }
        #endregion


    }

}

还有我的 XAML:

<Window.Resources>
        <ObjectDataProvider x:Key="objDs"
                            ObjectType="{x:Type data:ListDataAccess}"
                            MethodName="GetProducts">

        </ObjectDataProvider>



    </Window.Resources>



    <Grid DataContext="{Binding Source={StaticResource objDs}}">

        <DataGrid Margin="12,58,12,188" AutoGenerateColumns="False" Name="dataGrid1" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="true"
                  ColumnWidth="auto" HorizontalContentAlignment="Center" CanUserSortColumns="False" CanUserReorderColumns="False" CanUserAddRows="True" CellEditEnding="dataGrid1_CellEditEnding">

            <DataGrid.Columns>
                <DataGridTextColumn Width="2*" Header="#ID" Binding="{Binding Mode=TwoWay, Path=prodID, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True}" ></DataGridTextColumn>
                <DataGridTextColumn Width="4*" Header="Descripció" Binding="{Binding Mode=TwoWay, Path=prodName}" IsReadOnly="True" ></DataGridTextColumn>
                <DataGridTextColumn Width="1*" Header="Color" Binding="{Binding Mode=TwoWay, Path=prodColor}" IsReadOnly="True"></DataGridTextColumn>
                <DataGridTextColumn Width="1*" Header="Talla" Binding="{Binding Mode=TwoWay, Path=prodSize}" IsReadOnly="True"></DataGridTextColumn>
                <DataGridTextColumn Width="1*" Header="Quantitat" Binding="{Binding Mode=TwoWay, Path=prodQuantity}"></DataGridTextColumn>
                <DataGridTextColumn Width="2*" Header="Preu" Binding="{Binding Mode=TwoWay, Path=prodPrice, StringFormat=F}" IsReadOnly="True" ></DataGridTextColumn>
                <DataGridTextColumn Width="1*" Header="% Dpt" Binding="{Binding Mode=TwoWay, Path=prodDiscount}"></DataGridTextColumn>
                <DataGridTextColumn Width="2*" Header="Import" Binding="{Binding Mode=TwoWay, Path=prodSubtotal}" IsReadOnly="True"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>

【问题讨论】:

  • 如果你不想做验证检查值:e.Cancel = true;
  • 告诉我你要做什么?
  • 我已经用一些来源更新了我的问题。能不能请看一下就明白了。 〜你会把e.Cancel放在哪里?
  • 我复制了代码,但 出错了。

标签: c# wpf datagrid


【解决方案1】:

在 ObservableCollection 类型中实现 INotifyPropertyChangedIDataErrorInfo 接口。

【讨论】:

  • 我使用 INotifyPropertyChanged 和 IDataErrorInfo 实现,但是当我输入无效值时,调用 CellEditEnding =\
  • UpdateSource 应该在 propertychanged 上,否则它不会做检查
猜你喜欢
  • 1970-01-01
  • 2013-08-18
  • 1970-01-01
  • 2018-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
相关资源
最近更新 更多