【发布时间】:2014-01-10 18:17:46
【问题描述】:
我的 WP7 应用程序允许用户回答其他人提出的问题。 问题是如何在问题下列出它们:目前我使用 MVVM 方法,它们都是 ObservableCollection 的一部分,与 LongListSelector 相关联。
<toolkit:LongListSelector ItemsSource="{Binding Items}" IsFlatList="True">
<toolkit:LongListSelector.DataContext>
<local:ResponsesViewModel/>
</toolkit:LongListSelector.DataContext>
<toolkit:LongListSelector.ItemTemplate>
<DataTemplate>
<local:BoxRisposta />
</DataTemplate>
</toolkit:LongListSelector.ItemTemplate>
</toolkit:LongListSelector>
每个项目都绑定在一个名为 BoxRisposta 的自定义用户控件中,该控件包含用于显示用户名、用户回复时间的文本框,以及包含回复的 RichTextBox。
响应列表是此 ViewModel 类的一部分。
Public Class ResponsesViewModel
Implements INotifyPropertyChanged
Private _Items As ObservableCollection(Of Risposta)
Public Property Items() As ObservableCollection(Of Risposta)
Get
Return _Items
End Get
Set(ByVal value As ObservableCollection(Of Risposta))
_Items = value
End Set
End Property
Public Sub New()
Me.Items = New ObservableCollection(Of Risposta)()
QuoteCommand = New ActionCommand(New Action(Of Object)(Sub(p)
MessageBox.Show("quoted")
End Sub))
End Sub
Private _QuoteCommand As ICommand
Public Property QuoteCommand As ICommand
Get
Return _QuoteCommand
End Get
Private Set(value As ICommand)
_QuoteCommand = value
End Set
End Property
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal propertyName As String)
Dim handler As PropertyChangedEventHandler = PropertyChangedEvent
If handler IsNot Nothing Then
handler(Me, New PropertyChangedEventArgs(propertyName))
End If
End Sub
End Class
Public Class ActionCommand
Implements ICommand
Private execAction As Action(Of Object)
Private canExecFunc As Func(Of Object, Boolean)
Public Sub New(execAction As Action(Of Object))
Me.execAction = execAction
End Sub
Public Sub New(execAction As Action(Of Object), canExecFunc As Func(Of Object, Boolean))
Me.execAction = execAction
Me.canExecFunc = canExecFunc
End Sub
Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
If canExecFunc IsNot Nothing Then
Return canExecFunc.Invoke(parameter)
Else
Return True
End If
End Function
Public Event CanExecuteChanged As System.EventHandler Implements ICommand.CanExecuteChanged
Public Sub Execute(parameter As Object) Implements ICommand.Execute
If execAction IsNot Nothing Then
execAction.Invoke(parameter)
End If
End Sub
End Class
你看到我在里面添加了一个命令吗?我想在我的自定义用户控件中添加一个按钮,该按钮用作“引用按钮”,并告诉 ViewModel 类正在引用响应并传递该响应的内容。
我被困在这里,我不知道如何在这种特定情况下绑定命令,我有一个项目列表,而不仅仅是一个对象(互联网上的大多数示例都显示)
【问题讨论】:
标签: vb.net windows-phone-7 mvvm windows-phone-8 observablecollection