Validation of a combobox in Silverlight 4
-
I'm trying to understand how validation works for a combo box when its ItemsSource is bound to a ObserableCollection of complex types. I am using RIA as the serivce to connect the client tier to the middle tier. Also not sure if this makes a difference the combobox control is inside a dataform. I have done alot of reading on this and found this article to be the most useful: http://www.run80.net/?p=93 So firstly my entity: I have a field decorated like so:
[Required]
public virtual long FrequencyId { get; set; }\[Include\] \[Association("TreatmentFrequencyToTreatmentRecordAssociation", "FrequencyId", "Id", IsForeignKey = true)\] public virtual TreatmentFrequency Frequency { get { return this.frequency; } set { this.frequency = value; if (value != null) { this.FrequencyId = value.Id; } } }
Now I belive that I cannot set the [Required] annotation on an association but instead on the foreign key id (what the above article says). The actual Treatment Frequency class looks like this:
public class TreatmentFrequency
{
[Key]
public virtual long Id { get; set; }\[Required\] \[StringLength(10)\] public virtual string Code { get; set; } \[Required\] \[StringLength(40)\] public virtual string Name { get; set; } public override bool Equals(object obj) { obj = obj as TreatmentFrequency; if (obj == null) { return false; } return this.Id == ((TreatmentFrequency)obj).Id; } public override int GetHashCode() { return this.Name.GetHashCode(); } }
I have overriden the Equals and GetHashCode method becuase in another article it said that when in a collection you need to override the equals to match on the key otherwise when you use SelectedItem although all the values would be the same between the item in the collection and the selecteditem they would be two different instances and thus not match with the default implementation of Equals. Now my xaml looks like this:
<df:DataField Label="Frequency">
<ComboBox SelectedItem="{Binding Path=CurrentItem.Frequency, Mode=TwoWay}" ItemsSource="{Binding Path=Frequenc -
I'm trying to understand how validation works for a combo box when its ItemsSource is bound to a ObserableCollection of complex types. I am using RIA as the serivce to connect the client tier to the middle tier. Also not sure if this makes a difference the combobox control is inside a dataform. I have done alot of reading on this and found this article to be the most useful: http://www.run80.net/?p=93 So firstly my entity: I have a field decorated like so:
[Required]
public virtual long FrequencyId { get; set; }\[Include\] \[Association("TreatmentFrequencyToTreatmentRecordAssociation", "FrequencyId", "Id", IsForeignKey = true)\] public virtual TreatmentFrequency Frequency { get { return this.frequency; } set { this.frequency = value; if (value != null) { this.FrequencyId = value.Id; } } }
Now I belive that I cannot set the [Required] annotation on an association but instead on the foreign key id (what the above article says). The actual Treatment Frequency class looks like this:
public class TreatmentFrequency
{
[Key]
public virtual long Id { get; set; }\[Required\] \[StringLength(10)\] public virtual string Code { get; set; } \[Required\] \[StringLength(40)\] public virtual string Name { get; set; } public override bool Equals(object obj) { obj = obj as TreatmentFrequency; if (obj == null) { return false; } return this.Id == ((TreatmentFrequency)obj).Id; } public override int GetHashCode() { return this.Name.GetHashCode(); } }
I have overriden the Equals and GetHashCode method becuase in another article it said that when in a collection you need to override the equals to match on the key otherwise when you use SelectedItem although all the values would be the same between the item in the collection and the selecteditem they would be two different instances and thus not match with the default implementation of Equals. Now my xaml looks like this:
<df:DataField Label="Frequency">
<ComboBox SelectedItem="{Binding Path=CurrentItem.Frequency, Mode=TwoWay}" ItemsSource="{Binding Path=FrequencThis was actually a simple problem in th end, I assumed that the [Required] annotation would check that the association was present and not null. It seems that all it actually does is check that in this case that FrequencyId is not null. And there was the problem in that I was using a long and not a nullable long (long?). Once I made the change to make them nullable the validation started working as expected even with the bindings which made no sense to me. If anyone could explain them that would be great! Phil