datagrid验证单元格

时间:2012-08-14 11:21:57

标签: c# wpf datagrid

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

示例:

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

这怎么可能?

我的班级:

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>

1 个答案:

答案 0 :(得分:0)

在ObservableCollection类型中实现INotifyPropertyChangedIDataErrorInfo接口。