【发布时间】:2010-07-23 16:53:27
【问题描述】:
我可能对 INotifyPropertyChanged 和 Silverlight 背后的目的感到困惑...
我有我在设计器中创建的 XAML:
<UserControl.Resources>
<CollectionViewSource x:Key="theViewSource" d:DesignSource="{d:DesignInstance my:transferObject, CreateList=True}" />
</UserControl.Resources>
还有这个
<Canvas DataContext="{StaticResource theViewSource}">
<sdk:DataGrid ItemsSource="{Binding}" Name="theDataGrid">
<sdk:DataGrid.Columns>
<!-- columns omitted from post -->
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Canvas>
而且我还有一些代码隐藏:
Private Sub control_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
If Not (System.ComponentModel.DesignerProperties.GetIsInDesignMode(Me)) Then
Dim myCollectionViewSource As System.Windows.Data.CollectionViewSource =
CType(Me.Resources("theViewSource"), System.Windows.Data.CollectionViewSource)
Dim myViewModel As New ViewModels.ViewModelThatHasAnObservableCollectionProperty()
myCollectionViewSource.Source = myViewModel.TheObservableCollection()
End If
End Sub
还有一个看起来像这样的 ViewModel:
Public Class ViewModelBase
Implements ComponentModel.INotifyPropertyChanged
Protected Sub RaisePropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
Public Class ViewModelThatHasAnObservableCollectionProperty
Inherits ViewModelBase
Public Sub RetrieveCollectionCompletedHandler(ByVal sender As Object, ByVal e As SilverlightConsumableService.RetrieveMyCollectionCompletedEventArgs)
Dim unobservable As List(Of TransferObjects.TheTransferObject) = e.Result
'I was hoping for this next line of code to force a refresh of the DataGrid
TheObservableCollection = New ObservableCollection(Of TransferObjects.transferObject)(unobservable)
End Sub
Public Sub New()
Dim theClient As New SilverlightConsumableService.SilverlightConsumableSoapClient
AddHandler theClient.RetrieveMyCollectionCompleted, AddressOf RetrieveCollectionCompletedHandler
theClient.RetrieveMyCollectionAsync()
End Sub
Private _theObservableCollection As ObservableCollection(Of TransferObjects.transferObject)
Public Property TheObservableCollection() As ObservableCollection(Of TransferObjects.transferObject)
Get
Return _theObservableCollection
End Get
Set(ByVal value As ObservableCollection(Of TransferObjects.transferObject))
_theObservableCollection = value
RaisePropertyChanged("TheObservableCollection")
End Set
End Property
End Class
结果是 Silverlight 呈现数据网格时没有结果,因为事件处理程序尚未填充 TheObservableCollection。这是可以预料的。我希望在异步调用 Web 服务之后,调用 TheObservableCollection 的 Setter,随后调用 RaisePropertyChanged。
我的期望是 Silverlight 会通过将 dataGrid 重新绑定到属性来处理这个问题......使用 INotifyPropertyChanged 来实现它的目的不是为了让我不必以编程方式重新绑定网格,还是我对此感到困惑那个?
【问题讨论】:
标签: silverlight-4.0 observablecollection inotifypropertychanged