没有为新行更新数据模板的组合框

时间:2013-02-15 15:29:01

标签: .net wpf vb.net mvvm

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

<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并使数据模板起作用。

0 个答案:

没有答案