.net 数据模板不为新行更新的组合框

.net 数据模板不为新行更新的组合框,.net,wpf,vb.net,mvvm,.net,Wpf,Vb.net,Mvvm,我有一个组合框,显示一个带有复选框的列表,它工作正常,只是在一天中,可用选项会增加,即,我将项目添加到数据绑定列表中 <ComboBox x:Name="cb" ItemsSource="{Binding Path=ActiveCommodities}" IsEditable="True" IsReadOnly="True" Horizontal

我有一个组合框,显示一个带有复选框的列表,它工作正常,只是在一天中,可用选项会增加,即,我将项目添加到数据绑定列表中

<ComboBox x:Name="cb"    ItemsSource="{Binding Path=ActiveCommodities}" 
                       IsEditable="True"
                       IsReadOnly="True" 
                      HorizontalAlignment="Left"
                  Text="Commodity Filter">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding IsSelected}" 
                                      Width="20" />
                        <TextBlock Text="{Binding Text}" 
                                       Width="100" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
我可以在get上设置一个断点,我看到值中的项目计数是正确的,但是当它上升时,组合框不会显示新的项目

我的所选项目列表如下所示

Public Class SelectableItemList
    Inherits SortedList(Of String, SelectableItem)
    Implements INotifyCollectionChanged

    Protected Sub RaiseCollectionChanged(action As NotifyCollectionChangedAction)
        RaiseEvent CollectionChanged(Me, New NotifyCollectionChangedEventArgs(action))
    End Sub

    Public Event CollectionChanged(sender As Object, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Implements System.Collections.Specialized.INotifyCollectionChanged.CollectionChanged


    Public Overloads Function Add(index As String, text As String) As Boolean
        If Not ContainsKey(index) Then
            Dim si As New SelectableItem(text, Me)
            Add(index, si)
            AddHandler si.PropertyChanged, AddressOf OnSelectionChanged
            RaiseCollectionChanged(NotifyCollectionChangedAction.Reset)
            Return True
        End If
        Return False
    End Function

End Class

我想问题是因为我绑定到了values集合?但是我不知道如何绑定到sortedcollection并使数据模板工作。

ActiveComodities应该是ObservableCollection而不是List。您可以在此处查看更多信息:将其切换为注释,因为它并不能完全回答您的问题:您的SelectableItemList实现INotifyCollectionChanged,但您返回的是Values集合,而Values集合没有实现。我建议您考虑如何直接返回IList,或者如果可能,使用内置的ObservableCollection,它支持INotifyCollectionChanged自动更改。我已切换到observable集合,现在使用默认视图和排序描述进行排序。我猜当WPF刷新列表中的组合框时,如果它是列表的同一个实例,它不会查找新项目?不,WPF实际上会获取新项目。问题是,您返回的类(值集合)没有实现INotifyCollectionChanged,因此WPF根本就没有再次调用您的属性getter。
Public Class SelectableItemList
    Inherits SortedList(Of String, SelectableItem)
    Implements INotifyCollectionChanged

    Protected Sub RaiseCollectionChanged(action As NotifyCollectionChangedAction)
        RaiseEvent CollectionChanged(Me, New NotifyCollectionChangedEventArgs(action))
    End Sub

    Public Event CollectionChanged(sender As Object, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Implements System.Collections.Specialized.INotifyCollectionChanged.CollectionChanged


    Public Overloads Function Add(index As String, text As String) As Boolean
        If Not ContainsKey(index) Then
            Dim si As New SelectableItem(text, Me)
            Add(index, si)
            AddHandler si.PropertyChanged, AddressOf OnSelectionChanged
            RaiseCollectionChanged(NotifyCollectionChangedAction.Reset)
            Return True
        End If
        Return False
    End Function

End Class