我正在尝试使用组合框构建一个小应用程序,问题是项目没有正确更新,有时2个项目有时只有4个项目可见,但项目计数正在正确更新。以下是xaml代码:
<ComboBox Name="NumbersCombo" Width="118">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
以下是页面加载时itemssource的代码,其中con.Numbers是字符串和Numbers类的字典,因此发送值用于附件:
NumbersCombo.ItemsSource = con.Numbers.Values
以下是将新项目添加到组合框的代码:
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
以下是该类的代码:
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
简单来说,组合框的gui没有显示项目,但项目计数是正确的,你能告诉我哪里出错了。谢谢。
答案 0 :(得分:2)
如果希望在特定ContactCon对象或ContactCon对象集合发生更改时自动更新组合框,则ContactCon类需要实现INotifyPropertyChanged,并且需要将ItemSource设置为实现INotifyCollectionChanged的集合类(这意味着你只需要设置一次ItemSource)。您可以在以下question的答案部分找到如何实现可观察词典。
进行这些更改还可以解决您看到的同步问题。
答案 1 :(得分:1)
我同意Dave +1
如果您需要字典来查找密钥的查找速度或唯一性,那么您需要一个实现CollectionChanged的字典。
另一种选择是使用ObservableCollections直接使用LINQ进行查找。如果你有10000个LINQ仍然很快查找。由于您没有在组合框上使用虚拟化,我怀疑列表并不大。