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