Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Validation of a combobox in Silverlight 4

Validation of a combobox in Silverlight 4

Scheduled Pinned Locked Moved WPF
wpfcsharpwcfregexquestion
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phillip Donegan
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • P Phillip Donegan

      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

      P Offline
      P Offline
      Phillip Donegan
      wrote on last edited by
      #2

      This 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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups