开发者

How do I get a DataGrid Cell to stop the user entering an incorrect value?

开发者 https://www.devze.com 2023-03-16 12:51 出处:网络
I have followed the article How to: Implement Validation with the DataGrid Control though it does not prevent the user leaving the cell, I wish to leave the cell focussed. Consider a cell that is boun

I have followed the article How to: Implement Validation with the DataGrid Control though it does not prevent the user leaving the cell, I wish to leave the cell focussed. Consider a cell that is bound to an integer value, trying to enter an alpha character will not allow focus to be removed from the cell.

My grid is as follows:

<DataGrid HeadersVisibility="Column"
          AutoGenerateColumns="False"
          CanUserAddRows="False"
          CanUserDeleteRows="False"
          CanUserResizeRows="False"
          ItemsSource="{Binding SelectedLines}">
    <DataGrid.RowValidationRules>
        <ExceptionValidationRule />
    </DataGrid.RowValidationRules>
    <DataGrid.Columns>
        <DataGridTextColumn IsReadOnly="True" Header="Name" Binding="{Binding Name}"/>
        <DataGridTextColumn IsReadOnly="False" Header="Qty"
                            Binding="{Binding Quantity, ValidatesOnExceptions=True}"/>
    </DataGrid.Columns>
</DataGrid>

My datasource is an ObservableCollection of an object like the following:

public class MyEntry : INotifyPropertyChanged /*, IEditableObject*/ {
    public MyEntry(string name) {
        this.Name = name;
        this.Quantity = 1;
    }
    public string Name {
        get;
        private set;
    }
    private int quantity;
    public int Quantity {
        get { return quantity; }
        set {
            if (quantity != value) {
                if (value < 1)
           开发者_如何转开发         throw new ArgumentException("Quantity may not be less than 1.");
                if (value > 100)
                    throw new ArgumentException("Quantity may not be more than 100.");
                quantity = value;

                OnPropertyChanged("Quantity");
            }
        }
    }

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName) {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion

    #region IEditableObject Members
    private int backupQuantity;
    private bool inEdit;
    public void BeginEdit() {
        if (inEdit) return;
        inEdit = true;
        backupQuantity = this.Quantity;
    }
    public void CancelEdit() {
        if (!inEdit) return;
        inEdit = false;
        this.Quantity = backupQuantity;
    }
    public void EndEdit() {
        if (!inEdit) return;
        inEdit = false;
        backupQuantity = 0;
    }
    #endregion
}

No matter what I do, the grid does not show there is an error, i.e. there is no red box around the cell, further I want to stop the user leaving focus. If I enter 'x' into the cell it behaves correctly, though if I enter -1, it does not commit the value because the grid will not allow me to try and edit another row and pressing Escape back on the original cell reverts it correctly.

I would also like to know if there is a property that the grid exposes that it is in error, this way I can bind any validation such as an Accept button not being enabled.


in your special case your datagrid has no chance to say there is something wrong because the underlying data not get noticed that is something wrong.

when you bind to a VM property of type int AND in your ui you set the value to xyz for example - then the only thing that raise is a bindingexception, but the xyz will never come down to your vm.

its a common problem in mvvm and wpf. most time i use strings and IDataErrorInfo interface in my viewmodels and masked textboxen for input in my UI. the benefit of this is that i can handle every input im my vm IDataErrorInterface. but i have a little bit more work to map the string to the right type of my model (e.g string in vm to int in model).

if you look at your output window while debugging you should see the bindingexception.

i know its not a real answer...

0

精彩评论

暂无评论...
验证码 换一张
取 消