简体   繁体   中英

Updating Dictionary Values adds Rows to DataGrid WPF

I´m using this implementation of ObservableDictionary.

When I´m Updating the Values to a specific Key, then my Dictionary is updated correctly but the DataGrid - which is bound to the Dictionary - has more rows.

My XAML:

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

And my ViewModel where I update the Dictionary.

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]++;
    }
}

After executing the Code my View looks like this but I would expect it looks like this

EDIT

Contents of 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; }
    }

and Gradings:

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

EDIT 2

Found a solution or workaround...

Instead of using an ObservableDictionary I use a List like:

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

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

With GradingNumbers like:

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

   public int Amount { get; set; }
}

Adding of the Values:

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

And the XAML:

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

I used this approach already for another view and it works quite good but I thought using an ObservableDictionary would be more elegant...

I´m still interested in seeing a solution which works with an ObservableDictionary.

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM