I am new to WPF. In our current project, we have added validation rules for all data entry fields for which we need validation. We also copied code (also located elsewhere here on stackoverflow) that recursively iterates over all the bindings and their validation rules to see if all the data is valid before the data is saved.
This is our code, which, in my opinion, is the place to solve our problem:
Public Function ValidateBindings(ByVal parent As DependencyObject) As Boolean
Dim valid As Boolean = True
Dim localValues As LocalValueEnumerator = parent.GetLocalValueEnumerator
While localValues.MoveNext
Dim entry As LocalValueEntry = localValues.Current
If BindingOperations.IsDataBound(parent, entry.Property) Then
Dim binding As Binding = BindingOperations.GetBinding(parent, entry.Property)
For Each rule In binding.ValidationRules
Dim result As ValidationResult = rule.Validate(parent.GetValue(entry.Property), Nothing)
If Not result.IsValid Then
Dim expression As BindingExpression = BindingOperations.GetBindingExpression(parent, entry.Property)
Validation.MarkInvalid(expression, New ValidationError(rule, expression, result.ErrorContent, Nothing))
valid = False
End If
Next
End If
End While
For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
If Not ValidateBindings(child) Then
valid = False
End If
Next
Return valid
End Function
I tried to figure out how to use the dependency GetValue()property , but my attempts have so far failed. Can someone help me with this or this wrong way of thinking to solve this problem?IsEnabledPropertyparent
, " ", , .
Binding.NotifyOnValidationError Binding XAML, IsEnabled NotifyOnValidationError, , DependencyProperty.
, , , ElementIsEnabled , - XAML:
<Binding.ValidationRules>
<local:MustContainInteger ElementIsEnabled="{Binding SameBindingAsIsEnabled}" />
</Binding.ValidationRules>
, ElementIsEnabled DependencyProperty DependencyObject.
, .