CollectionView Binding does not work with nullable foreign keys RRS feed

  • Question

  • Dear all,
    I am pulling my hair out as I don't understand why this is not working, please could someone assist me here. I will take a simple example:

    Let's say I have a table called PERSON with 3 columns
    SURNAME nvarchar not null, primary key,
    TITLE_ID int not null,       <- Foreign Key with the TITLE lookup table
    HAIR_COLOR_ID int null.  <- Foreign Key with the COLOR lookup table

    I have a lookup table TITLE with an ID and a NAME column, data can look as follows:
    1, MR.
    2, MRS.
    3, DR.  etc....

    I have a lookup table called COLOR with an ID and a NAME column, with the following data:
    1, BROWN
    2, RED,
    3, BLOND
    4, BLACK etc...

    Data for the PERSON table can look as follows:
    BRADFORD, 1, 4    <-  MR. BRADFORD with BLACK hair
    KELLY,2,null          <- MRS KELLY of which the hair color is not filled in

    In my WPF application I am using CollectionViews to bind the lookup tables eg.
    In the code behind:


    var titlelist = from fields in DataCont.Title


    select new { fields.ID, fields.Name };


    CollectionViewSource titlesource = (CollectionViewSource)FindResource("TitleLookup");

    titlesource.Source = titlelist;

    The XAML looks as follows:
    <ComboBox Text="{Binding Path=TITLE_ID, Mode=TwoWay}" Width="Auto" Height="26" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="5,6,5,0" BorderThickness="1,1,1,1" IsEditable="True"




    ItemsSource="{Binding Source={StaticResource TitleLookup}}"


    SelectedValue="{Binding Path=TITLE_ID}"




    DisplayMemberPath="Name" />

    Everything works 100% so far, the bindings work perfectly, BUT, the moment I follow the exact same logic for the COLOR lookup table and I basically copy and paste the code behind and XAML and change the names, the bindings on the COLOR dropbox DOES NOT WORK !!!! It populates the dropbox, but it behaves funny if I choose a color. Example: when I navigate to the next row, it would set the color on the previous row to null. Then, on the new row I can set the COLOR and save the record and it saves perfectly, but the moment I move to the next row, it sets the COLOR back to null again. 

    2 questions:
    1) In the intellisense I have noticed that when I build a LINQ query on the PERSON table and I hover over the HAIR_COLOR_ID column, it shows "int? PERSON.HAIR_COLOR_ID", but with the TITLE_ID column, it does not show the "?" after "int"  Does the "?" mean it is because that column allow nulls, or is there something else wrong and that is why the COLOR combobox binding does not work properly ? I have checked the LINQtoSQL classes, and nothing obvious is wrong there. I even tried to rename the tables and fields and re-generated the diagram, but the NULLABLE FOREIGN key keeps giving a problem with the bindings.

    2) In the COLOR combobox, there is no "blank" value, so how would one choose/enter a "null" value ?

    I am desperate, as my application prototype is only a small one with 12 tables and the NULL FOREIGN KEYS behaves the same with all the dropboxes, I can see nothing wrong (unless I have missed something else) and I am pulling my hair out, as I need to get an architecture up and runing for a huge VB6 application with nearly 500 tables to migrate to WPF.

    Any advice will be highly appreciated !!!

    Wednesday, June 10, 2009 8:08 PM

All replies

  • Try experimenting with changing the Mode property of the Binding. Based on your scenario, TwoWay doesn't make sense, as you want the value from the ComboBox to populate the value of your Person object, not the other way around. OneWay makes more sense. This could explain why you saw strange behavior with the drop down list rows being set to null.

    And your other questions--

    1. The "?" after the value type is syntactic sugar for a nullable type e.g. int? is equivalent to Nullable<int>
    2. You will have to put a value into the collection your drop down list binds to that represents null.
    Blog - Twitter - @ddewinter
    Thursday, June 11, 2009 3:17 PM
  • Friday, August 21, 2009 4:47 PM