Weird validation issue when binding to a Observable collection of type

Weird validation issue when binding to a Observable collection of type

Old forum URL: forums.lhotka.net/forums/t/10930.aspx


CrikeyMoses posted on Tuesday, November 29, 2011

Hi guys,

We are using Silverlight 4, Csla 4.1, MVVM and Bxf.

Background: We have custom validation that checks if the 'State' has been selected if someone lives in the USA, something like this:

public class ParticipantUSRule : Csla.Rules.BusinessRule
{
        protected override void Execute(Csla.Rules.RuleContext context)
        {
            var target = (Participant)context.Target;
            if (target.Country_of_Residence == "United States of America")
                if (string.IsNullOrEmpty(target.State))
                    context.AddErrorResult("State required for US country");
            base.Execute(context);
        }
}

That all works fine...

The issue we are getting is validation doesn't fire when we bind a combo box to an observable collection of a type BO (StateList, in my case), it does how ever work when we bind to an observable collection of type string, i'll post the two scenarios below to demonstrate: 

Firstly the working version:
In my ViewModel i have a Observable collection of type string which i bind to the itemsource property of a combo box. 
 public ObservableCollection<string> StatesList

 

 {
            get
            {
                var result = new ObservableCollection<string>();
                result.Add("Alabama");
                result.Add("Arizona");
                result.Add("Calafornia");
                return result;
            }
   }

For this my xaml binding would look like:

 <ComboBox Grid.Column="1" Grid.Row="11"  Name="cboState" Margin="5" SelectedItem="{Binding Path=Model.State, Mode=TwoWay,ValidatesOnExceptions=True,  NotifyOnValidationError=True}" ItemsSource="{Binding Path=StatesList}" />

 

Now that all works fine until we populate the StateList with values from the DB as follows:

public ObservableCollection<StateListVM> StatesList
{
            get
            {
                var result = new ObservableCollection<StateListVM>();
                result.Add(new StateListVM());
                return result;
            }
}

And for this my binding would change slightly to:
<ComboBox Grid.Column="1" Grid.Row="11"  Name="cboState" Margin="5" SelectedValue="{Binding Path=Model.State, Mode=TwoWay,ValidatesOnExceptions=True,  NotifyOnValidationError=True}" ItemsSource="{Binding Source={StaticResource participantVMStatesListModelViewSource}}" SelectedValuePath="State_Name" DisplayMemberPath="State_Name"/>

The binding seems to work fine as it is saving the data to the DB, just the validation we are struggling with.

Please help

UPDATE: The States loads and the CustomRule does fire, just no red border and ErrorInfo on the Combo Box control... Please help

 

JonnyBee replied on Tuesday, November 29, 2011

There is several bugfixes in PropertyStatus in Csla 4.2 so try and update this component first. 

CrikeyMoses replied on Thursday, December 01, 2011

No luck there Jonny. I still have the same issue after upgrading. Any other suggestions?

JonnyBee replied on Monday, December 05, 2011

Do you use the PropertyStatus control to show error messages?

It doesn't show in the code in your post.

 

CrikeyMoses replied on Friday, December 09, 2011

No I'm not using PropertyStatus, we are using the standard error message (with red border) from the ComboBox. I wouldn't think this makes a difference. Also when i did use the PropertyStatus control this didn't change anything.

Like i said the issue is when we are binding a Observable collection of type string (to the ComboBox), everything works fine and when we change the Observable collection to fetch from the DB it doesn't work, I think it has to do with the fact that the ViewModel (Participant) gets fetched BEFORE the fetch of the observable collection (StateList) has completed. Then the binding messes something up... (Because its trying to bind the Itemsource of the combobox after the SelectedValue has been set)

JonnyBee replied on Friday, December 09, 2011

Have you considered to use a UOW - approach.

Look at this post: http://forums.lhotka.net/forums/p/10968/51038.aspx#51038 

Copyright (c) Marimer LLC