0

我的WP7應用程序允許用戶回答他人提出的問題。 問題是如何在問題下列出它們:目前我使用MVVM方法,它們都是與LongListSelector關聯的ObservableCollection的一部分。命令綁定MVVM

<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自定義用戶控制,即cointains文本框以顯示用戶名,在該用戶的答覆時間,即cointains回覆一個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 

你是否看到我在裏面添加了一個Command?我想在我的自定義用戶控件中添加一個按鈕,作爲「引用按鈕」工作,並告訴ViewModel類響應被引用並傳遞響應的內容。

我堅持在這裏,我不kwnow如何命令在這種特殊情況下綁定在那裏我有一個項目列表,而不是一個對象(如對互聯網節目的例子大部分)

回答

0

我不得不通過父LLS的的DataContext:

<toolkit:LongListSelector.ItemTemplate> 
          <DataTemplate> 
           <local:BoxRisposta QuoteCommand="{Binding ElementName=ResponseList, 
           Path=DataContext.QuoteCommand}" DeleteCommand="{Binding ElementName=ResponseList, 
           Path=DataContext.DeleteCommand}"/> 
          </DataTemplate> 
</toolkit:LongListSelector.ItemTemplate> 
0

試試這個:

<local:BoxRisposta DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}"/> 

現在用戶控件使用相同的datacontext當前頁面

+0

不是這樣,我不能覆蓋已經用包含響應信息的類設置的DataContext。 – fillobotto