开发者

WPF - MVVM - NHibernate Validation

开发者 https://www.devze.com 2022-12-11 10:09 出处:网络
Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel.

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel.

I am using NHibernate to decorate my property on the domain object as below.

private decimal _refurbishmentFee;
[Min(Value=0), NotNull]
public decimal RefurbishmentFee
{
    get { return _refurbishmentFee; }
    set 
    {
        if (_refurbishmentFee == value) return;
        _refurbishmentFee = value;
        OnPropertyChanged("RefurbishmentFee");
    }
}

The validation works fine if I put in a number less than 0, however if I put in alpha characters or a s开发者_如何学JAVApace then nothing happens, the application doesn't even fall into the setter.

Any help?

Thanks Faisal


Yes I am using ValidatesOnDataErrors in the XAML. Fortunately one of my colleagues resolved this issue last night so I thought I should put it on here in case someone else is in the same position.

To get the validation to occur in all cases you need to add ValidatesOnExceptions="True" as well as ValidatesOnDataErrors="True" in the XAML.

<Controls:NumberTextBox IsEnabled="{Binding IsEditable}"  Grid.Row="1" Grid.Column="3" Name="txtRefurbishmentFee">
    <TextBox.Text>
        <Binding Path="Entity.RefurbishmentFee" UpdateSourceTrigger="PropertyChanged" ValidatesOnExceptions="True" ValidatesOnDataErrors="True" />
    </TextBox.Text>
</Controls:NumberTextBox>

Then on the domain object use standard NHibernate validation decorators.

private decimal _refurbishmentFee;
[Min(Value=0)]
public decimal RefurbishmentFee
{
    get { return _refurbishmentFee; }
    set 
    {
        if (_refurbishmentFee == value) return;
        _refurbishmentFee = value;
        OnPropertyChanged("RefurbishmentFee");
    }
}

Thanks, Faisal

0

精彩评论

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