更新字典值会将行添加到DataGrid WPF

时间:2019-03-05 07:55:45

标签: c# wpf mvvm wpfdatagrid

我正在使用this的ObservableDictionary实现。

当我将值更新为特定键时,我的字典将更新 correctly,但绑定到Dictionary的DataGrid具有更多行。

我的XAML:

<DataGrid ItemsSource="{Binding GradeNumbers}"
            AutoGenerateColumns="False">
      <DataGrid.Columns>                    
          <DataGridTextColumn Header="Amount" Binding="{Binding Value}" IsReadOnly="True"/>
      </DataGrid.Columns>
</DataGrid>

还有更新字典的我的ViewModel。

public ObservableDictionary<double, int> GradeNumbers { get; set; }

private void GetGradeNumbers()
{
    foreach (var gradeRating in this.gradeRatings)
    {
      this.gradeNumbers.Add(gradeRating.Grade, 0);
    }

    foreach (var grading in this.gradings)
    {
       this.gradeNumbers[grading.Grade]++;
    }
}

执行代码后,我的视图看起来像this,但我希望它看起来像this

编辑

GradeRaitings的内容

public List<GradeRatingDTO> GradeRatings
    {
        get
        {
            return this.gradeRatings;
        }

        set
        {
            this.gradeRatings = value;
            this.OnPropertyChanged(nameof(this.GradeRatings));
        }
    }
public class GradeRatingDTO
    {
        /// <summary>
        /// Gets or sets the Grade.
        /// </summary>
        public double Grade { get; set; }

        /// <summary>
        /// Gets or sets the LowerBoundary of the Grade.
        /// </summary>
        public double LowerBoundary { get; set; }

        /// <summary>
        /// Gets or sets the UpperBoundary of the Grade.
        /// </summary>
        public double UpperBoundary { get; set; }
    }

和评分:

private List<GradingModel> gradings;

public class GradingModel : ViewModelBase
    {
        private double grade;
        private double totalScore;

        /// <summary>
        /// Gets or sets the MatriculationNumber.
        /// </summary>
        public string MatriculationNumber { get; set; }

        /// <summary>
        /// Gets or sets the PointsPerProblems.
        /// </summary>
        public ObservableCollection<DoubleItem> PointsPerProblems { get; set; }

        /// <summary>
        /// Gets or sets the Grade.
        /// </summary>
        public double Grade
        {
            get
            {
                return this.grade;
            }

            set
            {
                this.grade = value;
                this.OnPropertyChanged(nameof(this.Grade));
            }
        }

        /// <summary>
        /// Gets or sets the TotalScore.
        /// </summary>
        public double TotalScore
        {
            get
            {
                return this.totalScore;
            }

            set
            {
                this.totalScore = value;
                this.OnPropertyChanged(nameof(this.TotalScore));
            }
        }

编辑2

找到了解决方案或解决方法...

我使用像这样的列表来代替使用ObservableDictionary:

public List<GradingNumbers> GradeNumbersList
{
  get
  {
    return this.gradeNumbersList;
  }

  set
  {
    this.gradeNumbersList = value;
    this.OnPropertyChanged(nameof(this.GradeNumbersList));
  }
 }

具有诸如以下的GradingNumbers:

public class GradingNumbers
{
   public double Grade { get; set; }

   public int Amount { get; set; }
}

添加值:

private void GetGradeNumbers()
{
  List<GradingNumbers> tmpGradingNumbers = new List<GradingNumbers>();

   foreach (var gradeRating in this.gradeRatings)
   {
     tmpGradingNumbers.Add(new GradingNumbers() { Grade = gradeRating.Grade, Amount = 0 });
    }

    foreach (var grading in this.gradings)
    {
      tmpGradingNumbers.First(g => g.Grade == grading.Grade).Amount++;
    }

    this.GradeNumbersList = tmpGradingNumbers;
 }

还有XAML:

<DataGrid ItemsSource="{Binding GradeNumbersList}"
                      AutoGenerateColumns="False">
      <DataGrid.Columns>
         <DataGridTextColumn Header="Amount" Binding="{Binding Amount}" IsReadOnly="True"/>
      </DataGrid.Columns>
 </DataGrid>

我已经将这种方法用于另一种视图,并且效果很好,但是我认为使用ObservableDictionary会更优雅...

我仍然希望看到一个与ObservableDictionary一起使用的解决方案。

1 个答案:

答案 0 :(得分:0)

找到答案并将其放在问题的“编辑”中。