【发布时间】:2015-01-11 21:04:07
【问题描述】:
我有一个可观察的集合,它为单选按钮提供值。当用户从一个项目移动到另一个项目时,如果用户之前选择了相应的组单选按钮,我希望自动选择它。
总而言之,这是一个多问题类型的考试,我希望在用户从一个问题导航到另一个问题时保持选中单选按钮。
这是视图:
<ItemsControl ItemsSource="{Binding CurrentQuestion.Choices}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding .}"
Command="{Binding DataContext.SaveAnswerCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
CommandParameter="{Binding}"
GroupName="choices"
/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
可观察的集合:
public static ObservableCollection<ExamModel> Questions
{
get
{
return _questions;
}
set
{
_questions = value;
}
}
//this is how i keep track of which item is currently active
public ExamModel CurrentQuestion
{
get { return _currentQuestion; }
set
{
if (_currentQuestion != value)
{
_currentQuestion = value;
OnPropertyChanged("CurrentQuestion");
}
}
}
模型:
public List<string> Choices
{
get
{
if (_choices == null)
_choices = new List<string>();
return _choices;
}
set
{
if (_choices != value)
{
_choices = value;
OnPropertyChanged("Choices");
}
}
}
【问题讨论】:
标签: c# wpf mvvm data-binding radio-button