In order to try to be generic I implemented that WPF and C# Binding in the view but the problem is that the setter of the Value property in the Item class is called two times successively (without passing in any other function - I checked the call stack)
I don't really know if it's a problem of binding or a problem in the code but If you have any idea I would like to hear your feedback.
<DataGrid Grid.Row="1" ItemsSource="{Binding Questions}" AutoGenerateColumns="False" SelectedItem="{Binding Path=DataContext.Answer.QuestionItem, RelativeSource={RelativeSource AncestorType=UserControl}}" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Questions" Width="SizeTocells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Question}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Answers" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
<ListView ItemsSource="{Binding Path=DataContext.Answer.Answers, Mode=OneWay, RelativeSource={RelativeSource AncestorType=UserControl}}">
<ListView.ItemTemplate>
<DataTemplate>
<RadioButton GroupName="{Binding Path=GroupName, Mode=OneWay}" Content="{Binding Path=Content, Mode=OneWay}" IsChecked="{Binding Path=Value, Mode=TwoWay}" Margin="0, 0, 10, 0" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
public class Item : BindableBase
{
public string Content { get; }
public string GroupName { get; }
private bool _val;
public bool Value
{
get { return _val; }
set
{
SetProperty(ref _val, value);
}
}
public Item(string content, string groupName, bool value = false)
{
Content = content;
GroupName = groupName;
Value = value;
}
}
public class AnswerModel
{
public List<Item> Answers { get; }
public AnswerModel(List<string> possibleAnswers)
{
Answers = new List<Item>();
for (int i = 0; i < possibleAnswers.Count(); ++i)
{
char c = Convert.ToChar('A' + i);
var letter = Convert.ToString(c);
Answers.Add(new Item(letter, "group" + letter));
}
}
}
public class InsertWordQuestionViewModel : BindableBase, INavigationAware
{
public AnswerModel Answer { get; private set; }
public void OnNavigatedTo(NavigationContext navigationContext)
{
PossibleAnswers = new List<string>() { "A test", "B test2", "C test3"};
Questions = navigationContext.Parameters["questions"] as List<QuestionModel<string, string>>;
Answer = new AnswerModel(PossibleAnswers);
}
}
Value
property in the Item class is called two times successively - I guess it happens in different items: one item/RadioButton gets unchecked, and another item/RadioButton gets checked. so value
argument should be different for those two consequtive invocations
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.