我有以下内容:
<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 没有成功
帮助!
谢谢安迪