0

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

<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>

我绑定到一个属性,该属性公开排序列表的值集合

   Public ReadOnly Property ActiveCommodities As IList(Of SelectableItem)
        Get
            If Not _activeCommodities Is Nothing Then
                Return _activeCommodities.Values
            End If
            Return Nothing
        End Get
    End Property

我可以在 get 上设置一个断点,我看到 Values 中的项目计数是正确的,但是当它上升时,组合框不会显示新项目。

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

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

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

4

0 回答 0