views:

68

answers:

0

Hello

I am having trouble firing a validation rule in the context laid out below.

From a visual standpoint I have a DataGrid with columns for every day of the week where a user can enter the number of hours worked on a given activity. There are also labels to display the total hours entered for each day, as well as for the week. It is an error to have worked more than 8 hr/day, which needs to show up in the labels representing the day in excess of 8 as well as the one for the week.

The binding for a given column for a day currently looks like:

<dg:DataGridTextColumn Header="Monday" Width="60" 
        CellStyle="{StaticResource DataEntryCellStyle}"
        Binding="{Binding Allocations[1].Amount, Converter={StaticResource amtConv}, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}"
                               />

And for the corresponding label for the total of the day is:

        <Label Style="{StaticResource TotalAmountStyle}" Validation.ErrorTemplate="{StaticResource validationTemplate}" >
        <Label.Content>
            <Binding Path="TotalAmounts[0]" Converter="{StaticResource amtConv}" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" ValidatesOnDataErrors="True" ValidatesOnExceptions="True">
                <Binding.ValidationRules>
                    <rules:OverQuotaRule />
                </Binding.ValidationRules>
            </Binding>
        </Label.Content>
    </Label>

The data part of the bindings work fine. The ViewModels are laid out such that:

  • WeeklyViewModel has ObservableCollection
  • ActivityViewModel has ObservableCollection
  • AllocationModel has double Amount

When the allocation Amount property changes, it fires a PropertyChange which updates the underlying BO's that existed prior to porting this to WPF. The BO knows whether the status of a given day is in error. This seemed to lend itself to setting up the OverQuotaRule you see in the total label's binding. The code for that is simply:

public class OverQuotaRule:ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo) {

        return ((TimeSheetBase) value).QuotaVerdict == WorkQuotaStatus.OverQuota
                   ? new ValidationResult(false, Strings.OverQuotaRule_InExcess)
                   : ValidationResult.ValidResult;
    }
}

But the rule never does get fired. Should it be attached to the cell that actually changes instead? I'm still trying to wrap my mind around WPF in general and validation in specific here, so any more general design ideas are most welcome.

Thanks in advance, and Cheers,
Berryl