1

I am trying to build a small application with a combobox and the issue is that the items are not getting updated in it properly, sometimes 2 items sometime 4 items are only getting visible however the items count is getting updated properly. Following is the xaml code:

                <ComboBox Name="NumbersCombo" Width="118">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Title}" />
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

Following is the code for itemssource on page load, where con.Numbers is dictionary of string and Numbers class, hence Values are sent for attachment:

NumbersCombo.ItemsSource = con.Numbers.Values

Following is the code for adding new items to combobox:

Dim temp As New BSPLib.ContactLib.ContactCon(con.prime.Conid, False)
con.Numbers.Add(temp.ConRowID, temp)
NumbersCombo.ItemsSource = con.Numbers.Values
TestLabel1.Content = NumbersCombo.Items.Count

Following is the code for the class:

Public Class ContactCon
        Property ConId As String
        Property ConRowID As String
        Property Title As String
        Property Mob1 As String
        Property Mob2 As String
        Property Land1 As String
        Property Land2 As String
        Property Email1 As String
        Property Email2 As String
        Property Fax1 As String
        Property Fax2 As String
        Property Primary As Boolean
        Public Sub New()
        End Sub

        Public Sub New(ByVal contactID As String, ByVal primarynumbers As Boolean)
            ConId = contactID
            ConRowID = contactID & "-" & Now.ToString
            If primarynumbers = True Then
                Title = "Primary Details"
            Else
                Title = "Additional Contact Numbers"
            End If
            Mob1 = ""
            Mob2 = ""
            Land1 = ""
            Land2 = ""
            Email1 = ""
            Email2 = ""
            Fax1 = ""
            Fax2 = ""
            Primary = primarynumbers
        End Sub
    End Class

    Public Class Contact
        Public prime As ContactPrime
        Public addrs As Dictionary(Of String, ContactAddress)
        Public Numbers As Dictionary(Of String, ContactCon)

        Public Sub New()
            Numbers = New Dictionary(Of String, ContactCon)

            'assigning initial ids and values
            Dim t As New ContactCon(prime.Conid, vbYes)
            Numbers.Add(t.ConRowID, t) 'Primary Contact Number
        End Sub

In simple, the gui of combo box is not showing the items but the items count is correct, could you please tell where I have gone wrong. Thank you.

surpavan
  • 1,372
  • 7
  • 34
  • 66
  • Do you see any databinding errors in the output pane when you start your program in debug mode in Visual Studio? (You need VS2010 to see those errors.) –  Mar 08 '12 at 08:52
  • @fmunkert, there are are no error in output window too, also I tried with try catch but none are shown. – surpavan Mar 08 '12 at 09:31
  • Are you adding the items in your dictionary at runtime i.e. after setting the dictionary values as an ItemSource for your combobox..?? – Rohit Vats Mar 08 '12 at 10:07

2 Answers2

2

If you want the combo box to be updated automatically when a particular ContactCon object or the collection of ContactCon objects changes then the ContactCon class needs to implement INotifyPropertyChanged and you need to set the ItemSource to a collection class that implements INotifyCollectionChanged (This means that you only have to set the ItemSource once). You can find out how to implement an observable dictionary in the answers section of the following question.

Making these changes will also fix the synchronisation problems you are seeing.

Community
  • 1
  • 1
Dave Maff
  • 798
  • 8
  • 12
  • Is there any other method to simplify it because the Inotify with Dictionary looks too big to code for a beginner. – surpavan Mar 08 '12 at 14:59
  • You can download an implementation (in c#) [here](http://drwpf.com/blog/Portals/0/Samples/ObservableDictionarySample.zip) – Dave Maff Mar 08 '12 at 15:27
  • Thank you, but I need some time to how how to use it, because I belong to VB. Yet that explains. Thank you. – surpavan Mar 08 '12 at 18:44
1

I agree with Dave +1

If you need dictionary for lookup speed or uniqueness on key then you need a dictionary that implements CollectionChanged.

The other option is to just use ObservableCollections direclty and use LINQ for lookup. If you have even 10000 LINQ is still pretty fast for lookup. Since you are not using virtualization on the combobox I suspect the list is not large.

paparazzo
  • 44,497
  • 23
  • 105
  • 176