我有以下不更新:組合框時,勢必CollectionViewSource,然後更新
<Window.Resources>
<CollectionViewSource Source="{Binding Categories}" x:Key="Categories"/>
</Window.Resources>
....
<ComboBox x:Name="cboCategory" Margin="170,125,0,0" ItemsSource="{Binding Source={StaticResource Categories}}" ItemTemplate="{StaticResource CategoryTemplate}" SelectedValue="{Binding Source={StaticResource Item}, Path=category}" SelectedValuePath="ID" Width="200" Style="{StaticResource RoundedComboBox}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
然後在代碼
Private _Categories As ObservableCollection(Of CategoryEntry)
Public Property Categories As ObservableCollection(Of CategoryEntry)
Get
Return _Categories
End Get
Set(value As ObservableCollection(Of CategoryEntry))
_Categories = value
End Set
End Property
.....
strSelect = "SELECT * FROM Categories WHERE Categories.comment<>'Reserved' ORDER BY Categories.comment"
dsccmd = New OleDbDataAdapter(strSelect, cn)
dsccmd.Fill(dsc, "Categories")
dvc = New DataView(dsc.Tables("Categories"))
_Categories = New ObservableCollection(Of CategoryEntry)(dvc.ToTable.AsEnumerable().[Select](Function(i) New [CategoryEntry](i("ID"), i("comment").TrimEnd(" "), i("work"), If(i("work"), New SolidColorBrush(Colors.CornflowerBlue), New SolidColorBrush(Colors.White)))))
Me.DataContext = Me
這工作得很好。但是,如果我改變_Categories的內容例如。使用上面的代碼設置_Categories = New ObservableCollection ......組合框未更新。
我一直在使用CollectionViewSource.GetDefaultView.Refresh和ComboBox.UpdateLayout沒有成功
幫助試過!
感謝 安迪
那麼,如果它「作品」之前,應該有INotifyPropertyChanged的在它和INotifyCollectionChanged應該已經在的ObservableCollection(提示中的ObservableCollection可觀察的) – 2013-04-23 23:11:00
就像我說的,但是當它被更新的集合將通知視圖。它將不會通知該視圖是否被替換。他正在創建一個新的可觀察集合,而不是改變現有集合。 – Kenneth 2013-04-23 23:22:20
它應該是'PropertyChangedEventArgs(「Categories」)',不帶下劃線。 – LPL 2013-04-23 23:33:43