绑定中的属性setter称为两次C#WPF

时间:2018-04-02 12:30:53

标签: c# wpf mvvm binding prism

为了尝试通用我在视图中实现了WPF和C#Binding,但问题是Item类中的Value属性的setter连续被调用两次(没有传入任何其他函数 - 我检查过调用堆栈)

我不知道这是绑定问题还是代码问题但如果您有任何想法我想听听您的意见。

<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);
     }
 }

1 个答案:

答案 0 :(得分:1)

Item类中的

Value属性连续被调用两次 - 我猜它发生在不同的项目中:一项/ RadioButton未经检查,另一项/ RadioButton被检查。因此value论证对于那两个连续的调用应该是不同的