Mark input as changed
This commit is contained in:
@ -24,7 +24,8 @@ namespace WGneu.Windows {
|
||||
private bool IsEditing = false;
|
||||
private bool IsCreating = false;
|
||||
private List<string> TextFilter = new();
|
||||
private readonly Dictionary<TextBox, bool> Valid = new();
|
||||
private readonly Dictionary<Control, bool> Valid = new();
|
||||
private readonly Dictionary<Control, object?> OriginalValues = new();
|
||||
private static readonly RoutedCommand CtrlF = new();
|
||||
private readonly WgContext Context = new();
|
||||
|
||||
@ -79,6 +80,17 @@ namespace WGneu.Windows {
|
||||
RefreshInputs();
|
||||
}
|
||||
|
||||
private void ClearInputStates() {
|
||||
foreach (var tb in Utils.FindVisualChilds<TextBox>(this))
|
||||
if (tb.Name != "SearchInput") Validator.ClearInputStatus(tb);
|
||||
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this))
|
||||
Validator.ClearInputStatus(cb);
|
||||
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this))
|
||||
Validator.ClearInputStatus(cb);
|
||||
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this))
|
||||
Validator.ClearInputStatus(rb);
|
||||
}
|
||||
|
||||
private void RefreshInputs() {
|
||||
Member m = (Member)MemberList.SelectedItem;
|
||||
if (m != null) {
|
||||
@ -90,6 +102,7 @@ namespace WGneu.Windows {
|
||||
DeleteMemberButton.IsEnabled = false;
|
||||
ClearInputs();
|
||||
}
|
||||
ClearInputStates();
|
||||
}
|
||||
|
||||
private void InitInputs() {
|
||||
@ -206,10 +219,12 @@ namespace WGneu.Windows {
|
||||
}
|
||||
|
||||
private void ResetButton_Click(object sender, RoutedEventArgs e) {
|
||||
if (IsEditing)
|
||||
if (IsEditing) {
|
||||
RefreshInputs();
|
||||
else if (IsCreating)
|
||||
} else if (IsCreating) {
|
||||
InitInputs();
|
||||
}
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void CancelButton_Click(object sender, RoutedEventArgs e) {
|
||||
@ -241,7 +256,7 @@ namespace WGneu.Windows {
|
||||
}
|
||||
|
||||
private void ShowSaveResetCancelButtons() {
|
||||
SaveButton.IsEnabled = true;
|
||||
SaveButton.IsEnabled = false;
|
||||
ResetButton.IsEnabled = false;
|
||||
CancelButton.IsEnabled = true;
|
||||
SaveButton.Visibility = Visibility.Visible;
|
||||
@ -299,6 +314,8 @@ namespace WGneu.Windows {
|
||||
}
|
||||
|
||||
private void FillInputs(Member m) {
|
||||
OriginalValues.Clear();
|
||||
|
||||
MgNrInput.Text = m.MgNr.ToString();
|
||||
PredecessorMgNrInput.Text = m.PredecessorMgNr.ToString();
|
||||
PrefixInput.Text = m.Prefix;
|
||||
@ -346,6 +363,15 @@ namespace WGneu.Windows {
|
||||
case "post": ContactPostInput.IsChecked = true; break;
|
||||
case "email": ContactEmailInput.IsChecked = true; break;
|
||||
}
|
||||
|
||||
foreach (var tb in Utils.FindVisualChilds<TextBox>(this))
|
||||
if (tb.Name != "SearchInput") OriginalValues[tb] = tb.Text;
|
||||
foreach (var cb in Utils.FindVisualChilds<ComboBox>(this))
|
||||
OriginalValues[cb] = cb.SelectedItem;
|
||||
foreach (var cb in Utils.FindVisualChilds<CheckBox>(this))
|
||||
OriginalValues[cb] = (cb.IsChecked ?? false) ? bool.TrueString : null;
|
||||
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this))
|
||||
OriginalValues[rb] = (rb.IsChecked ?? false) ? bool.TrueString : null;
|
||||
}
|
||||
|
||||
private void ClearInputs() {
|
||||
@ -357,6 +383,7 @@ namespace WGneu.Windows {
|
||||
cb.IsChecked = false;
|
||||
foreach (var rb in Utils.FindVisualChilds<RadioButton>(this))
|
||||
rb.IsChecked = false;
|
||||
OriginalValues.Clear();
|
||||
}
|
||||
|
||||
private bool IsValid() {
|
||||
@ -371,8 +398,28 @@ namespace WGneu.Windows {
|
||||
SaveButton.IsEnabled = (v && ch);
|
||||
}
|
||||
|
||||
private bool InputHasChanged(Control input) {
|
||||
if (!OriginalValues.ContainsKey(input)) {
|
||||
return false;
|
||||
} else if (input is TextBox tb) {
|
||||
return OriginalValues[tb]?.ToString() != tb.Text;
|
||||
} else if (input is ComboBox sb) {
|
||||
return OriginalValues[sb] != sb.SelectedItem;
|
||||
} else if (input is CheckBox cb) {
|
||||
return (OriginalValues[cb] != null) != (cb.IsChecked ?? false);
|
||||
} else if (input is RadioButton rb) {
|
||||
return (OriginalValues[rb] != null) != (rb.IsChecked ?? false);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private bool HasChanged() {
|
||||
return true; // TODO
|
||||
return !IsValid() ||
|
||||
Utils.FindVisualChilds<TextBox>(this).Any(InputHasChanged) ||
|
||||
Utils.FindVisualChilds<ComboBox>(this).Any(InputHasChanged) ||
|
||||
Utils.FindVisualChilds<CheckBox>(this).Any(InputHasChanged) ||
|
||||
Utils.FindVisualChilds<RadioButton>(this).Any(InputHasChanged);
|
||||
}
|
||||
|
||||
private void UpdatePlz(TextBox plzInput, ComboBox ortInput) {
|
||||
@ -390,10 +437,15 @@ namespace WGneu.Windows {
|
||||
private void InputTextChanged(TextBox input, bool optional, Func<TextBox, bool, ValidationResult> checker) {
|
||||
var res = checker(input, optional);
|
||||
Valid[input] = res.IsValid;
|
||||
if (res.IsValid)
|
||||
Validator.SetInputValid(input);
|
||||
else
|
||||
if (res.IsValid) {
|
||||
if (InputHasChanged(input)) {
|
||||
Validator.SetInputChanged(input);
|
||||
} else {
|
||||
Validator.ClearInputStatus(input);
|
||||
}
|
||||
} else {
|
||||
Validator.SetInputInvalid(input);
|
||||
}
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
@ -403,7 +455,44 @@ namespace WGneu.Windows {
|
||||
MessageBox.Show(res.ErrorContent.ToString(), msg ?? res.ErrorContent.ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
}
|
||||
|
||||
private void CheckBox_Changed(object sender, RoutedEventArgs e) {
|
||||
var input = (CheckBox)sender;
|
||||
if (InputHasChanged(input)) {
|
||||
Validator.SetInputChanged(input);
|
||||
} else {
|
||||
Validator.ClearInputStatus(input);
|
||||
}
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void RadioButton_Changed(object sender, RoutedEventArgs e) {
|
||||
var input = (RadioButton)sender;
|
||||
if (InputHasChanged(input)) {
|
||||
Validator.SetInputChanged(input);
|
||||
} else {
|
||||
Validator.ClearInputStatus(input);
|
||||
}
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void TextBox_TextChanged(object sender, RoutedEventArgs e) {
|
||||
var input = (TextBox)sender;
|
||||
if (InputHasChanged(input)) {
|
||||
Validator.SetInputChanged(input);
|
||||
} else {
|
||||
Validator.ClearInputStatus(input);
|
||||
}
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void ComboBox_SelectionChanged(object sender, RoutedEventArgs e) {
|
||||
var input = (ComboBox)sender;
|
||||
if (InputHasChanged(input)) {
|
||||
// TODO not working
|
||||
Validator.SetInputChanged(input);
|
||||
} else {
|
||||
Validator.ClearInputStatus(input);
|
||||
}
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user