正在使用mvvm模式的Windows Phone 8项目。我有一个LongListSelector作为我在视图上的控件之一,它绑定到一个Answers列表。当用户从列表中选择答案时,必须将参数发送到视图模型以验证所选答案是否正确。我遇到的问题是指定什么作为命令参数,我已经尝试了selectedItem但它向视图模型发送了null。任何帮助表示赞赏。
这是我的xaml
<StackPanel Orientation="Vertical" >
<StackPanel Orientation="Horizontal" Background="WhiteSmoke">
<Image Height="100" Width="100" Source="{Binding CurrentQuestion.Graphic}" />
<TextBlock Text="{Binding CurrentQuestion.Statement}" FontSize="30" Foreground="Black" />
</StackPanel>
<StackPanel>
<phone:LongListSelector
ItemsSource="{Binding CurrentQuestion.Answers}"
SelectionChanged="LongListSelector_SelectionChanged"
x:Name="Answers"
>
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,10,0,17" Background="{Binding Status, Converter={StaticResource StatusConverter}}" >
<Button
Content="{Binding Text}"
Height="100"
Width="auto"
Foreground="Black"
FontSize="18"
Command="{Binding ElementName=Answers, Path=DataContext.ProcessQuestionCommand}"
CommandParameter="{Binding Path=ItemTemplate.Selecteditem}"
/>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</StackPanel>
</StackPanel>
</Grid>
这是我的viewmodel的一部分
public ICommand ProcessQuestionCommand
{
get
{
if (_processQustionCommand==null)
{
_processQustionCommand = new RelayCommand<int>(ProcessQuestion, CanProcessQuestion);
}
return _processQustionCommand;
}
}
public ICommand ProcessQuestionCommand
{
get
{
if (_processQustionCommand==null)
{
_processQustionCommand = new RelayCommand<int>(ProcessQuestion, CanProcessQuestion);
}
return _processQustionCommand;
}
}
private bool CanProcessQuestion(int answerId)
{
//int answerId = Convert.ToInt32(answerIdStr);
if (answerId> CurrentQuestion.Answers.Count()-1)
{
return false;
}
if (CurrentQuestion.Answers[answerId].Status!=AnswerStatus.UNANSWERED)
{
return false;
}
return true;
}
private void ProcessQuestion(int answerId)
{
//int answerId = Convert.ToInt32(answerIdStr);
int correctAns = _currentQuestion.GetRightAnswer();
if (answerId != correctAns)
{
_currentQuestion.Answers[answerId].Status = AnswerStatus.INCORRECT;
}
else
{
_currentQuestion.Answers[answerId].Status = AnswerStatus.CORRECT;
if (Test.Count > _questionCount)
{
_questionCount++;
CurrentQuestion = Test[_questionCount];
}
else
{
//tell view to finish up and move screens
// show a popup alerting the user that the practise has ended
}
}
}
我的命令类
public class RelayCommand<T>:ICommand
{
private readonly Action<T> _executeAction;
private readonly Func<T, bool> _canExecuteMethod;
public RelayCommand(Action<T> executeAction)
:this(executeAction,null)
{
}
public RelayCommand(Action<T> executeAction, Func<T,bool> canExecuteMethod)
{
if (executeAction==null)
{
throw new ArgumentNullException("executeAction");
}
this._executeAction = executeAction;
this._canExecuteMethod = canExecuteMethod;
}
public bool CanExecute(T parameter)
{
if (_canExecuteMethod!= null)
{
return _canExecuteMethod(parameter);
}
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(T parameter)
{
if (_executeAction!= null)
{
_executeAction(parameter);
}
}
public bool CanExecute(object parameter)
{
return CanExecute((T)parameter);
// return true;
}
public void Execute(object parameter)
{
Execute((T)parameter);
// _executeAction((T)parameter);
}
}
答案 0 :(得分:0)
你不能将CommandParameter绑定到afaik,那么为什么不通过将SelectedItem作为参数传递而完全避免这个问题,而是将它绑定到ViewModel上的SelectedAnswer属性,然后直接在ProcessQuestion命令逻辑中访问它