Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing bugs 1260528 and 1260525 #366

Merged
merged 8 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
changed to use OnListItemsChanged event
  • Loading branch information
Felipe da Conceicao Guimaraes committed May 10, 2021
commit bd2c8b0a41d093add2365d9f9243c1add279d4e2
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ public partial class CreateExpenseReportDialogBox : Window
public CreateExpenseReportDialogBox()
{
InitializeComponent();

var app = Application.Current;
var expenseReport = (ExpenseReport) app.FindResource("ExpenseData");
expenseReport?.EnsureInitialized();
}

private void addExpenseButton_Click(object sender, RoutedEventArgs e)
Expand Down
14 changes: 0 additions & 14 deletions Sample Applications/ExpenseIt/ExpenseItDemo/ExpenseReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,13 @@ public class ExpenseReport : INotifyPropertyChanged
private string _costCenter;
private string _employeeNumber;
private int _totalExpenses;
private bool _initialized = false;

public ExpenseReport()
{
LineItems = new LineItemCollection();
LineItems.LineItemCostChanged += OnLineItemCostChanged;
}

/*
* This method is needed because this app operates on a singleton ExpenseReport.
* It preloads that report with real LineItems directly from the markup.
* Doing that, the markup call IList.Add avoiding the override created on the class.
* So, we need to ensure the List class subscribes to the Item's events after its creation.
* */
public void EnsureInitialized()
{
if (_initialized) return;
_initialized = true;
LineItems.InitializeItems();
}

public string Alias
{
get { return _alias; }
Expand Down
26 changes: 14 additions & 12 deletions Sample Applications/ExpenseIt/ExpenseItDemo/LineItemCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// // Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace ExpenseItDemo
Expand All @@ -11,28 +13,28 @@ public class LineItemCollection : ObservableCollection<LineItem>
{
public event EventHandler LineItemCostChanged;

public void InitializeItems()
public LineItemCollection()
{
for(int i = 0; i < Items.Count; i++)
{
Items[i].PropertyChanged += LineItemPropertyChanged;
}
CollectionChanged += OnListItemsChanged;
}

public new void Add(LineItem item)
private void LineItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (item != null)
if (e.PropertyName == "Cost")
{
item.PropertyChanged += LineItemPropertyChanged;
OnLineItemCostChanged(this, new EventArgs());
}
base.Add(item);
}

private void LineItemPropertyChanged(object sender, PropertyChangedEventArgs e)
private void OnListItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.PropertyName == "Cost")
if(e.Action == NotifyCollectionChangedAction.Add)
{
OnLineItemCostChanged(this, new EventArgs());
for(int i = 0; i < e.NewItems.Count; i++)
{
LineItem item = e.NewItems[i] as LineItem;
item.PropertyChanged += LineItemPropertyChanged;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to use the Add event of the list. This version is working well.

}
}

Expand Down