diff --git a/Sample Applications/ExpenseIt/ExpenseItDemo/CreateExpenseReportDialogBox.cs b/Sample Applications/ExpenseIt/ExpenseItDemo/CreateExpenseReportDialogBox.cs index b728084fc..21dcff8f8 100644 --- a/Sample Applications/ExpenseIt/ExpenseItDemo/CreateExpenseReportDialogBox.cs +++ b/Sample Applications/ExpenseIt/ExpenseItDemo/CreateExpenseReportDialogBox.cs @@ -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( + "Expense Report Created!", + "ExpenseIt Standalone", + MessageBoxButton.OK, + MessageBoxImage.Information); - DialogResult = true; + DialogResult = true; + } } private void cancelButton_Click(object sender, RoutedEventArgs e) diff --git a/Sample Applications/ExpenseIt/ExpenseItDemo/LineItemCollection.cs b/Sample Applications/ExpenseIt/ExpenseItDemo/LineItemCollection.cs index 3729a3078..ac2552b95 100644 --- a/Sample Applications/ExpenseIt/ExpenseItDemo/LineItemCollection.cs +++ b/Sample Applications/ExpenseIt/ExpenseItDemo/LineItemCollection.cs @@ -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 @@ -11,13 +13,9 @@ public class LineItemCollection : ObservableCollection { public event EventHandler LineItemCostChanged; - public new void Add(LineItem item) + public LineItemCollection() { - if (item != null) - { - item.PropertyChanged += LineItemPropertyChanged; - } - base.Add(item); + CollectionChanged += OnListItemsChanged; } private void LineItemPropertyChanged(object sender, PropertyChangedEventArgs e) @@ -28,6 +26,18 @@ private void LineItemPropertyChanged(object sender, PropertyChangedEventArgs e) } } + private void OnListItemsChanged(object sender, NotifyCollectionChangedEventArgs e) + { + if(e.Action == NotifyCollectionChangedAction.Add) + { + for(int i = 0; i < e.NewItems.Count; i++) + { + LineItem item = e.NewItems[i] as LineItem; + item.PropertyChanged += LineItemPropertyChanged; + } + } + } + private void OnLineItemCostChanged(object sender, EventArgs args) { LineItemCostChanged?.Invoke(sender, args); diff --git a/Sample Applications/ExpenseIt/ExpenseItDemo/Styles.xaml b/Sample Applications/ExpenseIt/ExpenseItDemo/Styles.xaml index 9bf08438d..428f20579 100644 --- a/Sample Applications/ExpenseIt/ExpenseItDemo/Styles.xaml +++ b/Sample Applications/ExpenseIt/ExpenseItDemo/Styles.xaml @@ -392,6 +392,7 @@ + diff --git a/Sample Applications/ExpenseIt/ExpenseItDemo/ViewChartWindow.xaml b/Sample Applications/ExpenseIt/ExpenseItDemo/ViewChartWindow.xaml index d798a4005..8e48dd4b2 100644 --- a/Sample Applications/ExpenseIt/ExpenseItDemo/ViewChartWindow.xaml +++ b/Sample Applications/ExpenseIt/ExpenseItDemo/ViewChartWindow.xaml @@ -4,6 +4,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ExpenseItDemo" + DataContext="{StaticResource ExpenseData}" mc:Ignorable="d" Title="ViewChartWindow" Height="300" Width="300">