-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Changes from 5 commits
f4984a1
f05b1d9
bbf0b7a
539e12c
065cfa0
0247015
72d7ecd
bd2c8b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ private void addExpenseButton_Click(object sender, RoutedEventArgs e) | |
var app = Application.Current; | ||
var expenseReport = (ExpenseReport) app.FindResource("ExpenseData"); | ||
expenseReport?.LineItems.Add(new LineItem()); | ||
|
||
DataGridRow row =null; | ||
|
||
// Dispatching this at loaded priority so the new row has been added before our code runs | ||
|
@@ -82,15 +82,37 @@ private void viewChartButton_Click(object sender, RoutedEventArgs e) | |
dlg.Show(); | ||
} | ||
|
||
bool isValid(DependencyObject parent) | ||
{ | ||
if (System.Windows.Controls.Validation.GetHasError(parent)) return false; | ||
|
||
for(int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); i++) | ||
{ | ||
DependencyObject child = VisualTreeHelper.GetChild(parent, i); | ||
if (!isValid(child)) return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private void okButton_Click(object sender, RoutedEventArgs e) | ||
{ | ||
MessageBox.Show( | ||
"Expense Report Created!", | ||
"ExpenseIt Standalone", | ||
MessageBoxButton.OK, | ||
MessageBoxImage.Information); | ||
if(!isValid(expenseDataGrid1)){ | ||
MessageBox.Show( | ||
"Please, fix the errors.", | ||
"Error", | ||
MessageBoxButton.OK, | ||
MessageBoxImage.Error); | ||
} else | ||
{ | ||
MessageBox.Show( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another horrible UI design - message boxes should be a last resort only for very serious problems. But not worth changing here. |
||
"Expense Report Created!", | ||
"ExpenseIt Standalone", | ||
MessageBoxButton.OK, | ||
MessageBoxImage.Information); | ||
|
||
DialogResult = true; | ||
DialogResult = true; | ||
} | ||
} | ||
|
||
private void cancelButton_Click(object sender, RoutedEventArgs e) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,15 @@ public class LineItemCollection : ObservableCollection<LineItem> | |
{ | ||
public event EventHandler LineItemCostChanged; | ||
|
||
public new void InsertItem(int index, LineItem item) | ||
{ | ||
if (item != null) | ||
{ | ||
item.PropertyChanged += LineItemPropertyChanged; | ||
} | ||
base.InsertItem(index, item); | ||
} | ||
|
||
public new void Add(LineItem item) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this method - otherwise you'll add the PropertyChanged handler twice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose to revert the last modification and remain with the EnsureInitialize solution because the behaviour was not correct at all. With this version, athough we use more lines of code, the result seems to be the safest and it solves the bug. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was wrong? Moving the "add PropertyChanged listener" logic from Add to InsertItem should work, and eliminate the need for both EnsureInitialized and the "new Add" method. That uses fewer lines, but more importantly it demonstrates the right way to do the job. (The presence of the 'new' keyword is often a sign that you're doing something fishy.) All the IList and IList "add" methods - Add, Insert - end up calling InsertItem. Unless I'm missing something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I understand. I will try to change this one again to make it in the correct way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I was mistaken. The the "add PropertyChanged listener" in the InsertItem method didn't work. It seems that the list still doesn't subscribe to the events of the ones in the markup. |
||
{ | ||
if (item != null) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you really need all that? I think DataGrid won't leave edit-mode if there are validation errors, so all you have to do is check whether the DataGrid is editing. (Caution: I remember arguing about exactly this issue - knowing whether all validation errors have been cleared before proceeding with a higher-level commit like 'save' or 'close dialog' - but I don't remember how it turned out.)