我想验证一些控件,但前提是我的复选框被选中。(在这个例子中,我只显示一个文本框来验证)
我尝试使用 BindingGroup,但这意味着该复选框也已验证,这给了我错误的结果。
现在我尝试使用 MultiBinding:
<CheckBox Name="chkUseLqv"
Grid.Row="6"
Grid.Column="0"
Margin="0,0,8,0"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Content="Use LQV"
IsChecked="{Binding LqvConfigurationEnabled}" />
[...]
<GroupBox Grid.Row="1"
Grid.RowSpan="6"
Grid.Column="4"
Margin="5"
Header="LQV Configuration"
IsEnabled="{Binding LqvConfigurationEnabled}">
<Grid>
<TextBox Name="txtLqvDatabaseServer"
Grid.Row="1"
Grid.Column="1"
VerticalAlignment="Center"
Validation.ErrorTemplate="{StaticResource ValidationErrorTemplate}">
<TextBox.Text>
<MultiBinding Converter="{Converters:LqvConfigurationMultiBindingConverter}" UpdateSourceTrigger="PropertyChanged">
<MultiBinding.ValidationRules>
<LocalValidationRules:LqvDatabaseServerValidator />
</MultiBinding.ValidationRules>
<Binding ElementName="chkUseLqv" Path="IsChecked" />
<Binding Path="LqvDatabaseServer" />
</MultiBinding>
</TextBox.Text>
</TextBox>
</Grid>
</GroupBox>
我的验证器:
Imports System.Text.RegularExpressions
Namespace ValidationRules
Public Class LqvDatabaseServerValidator
Inherits ValidationRule
Public Overrides Function Validate(ByVal value As Object, ByVal cultureInfo As System.Globalization.CultureInfo) As System.Windows.Controls.ValidationResult
Dim useLqv = CType(value, Object()).OfType(Of Boolean).First()
Dim valueFromSource = CType(value, Object()).OfType(Of String).FirstOrDefault()
If useLqv Then
If String.IsNullOrEmpty(valueFromSource) Then
Return New ValidationResult(False, "This field is required!")
End If
If Not (
Regex.IsMatch(valueFromSource, "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$") OrElse
Regex.IsMatch(valueFromSource, "^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$")
) Then
Return New ValidationResult(False, "Invalid input!")
End If
End If
Return ValidationResult.ValidResult
End Function
End Class
End Namespace
转换器:
Imports System.Windows.Markup
Namespace Converters
Public Class LqvConfigurationMultiBindingConverter
Inherits MarkupExtension
Implements IMultiValueConverter
Public Sub New()
End Sub
Private _orig As Object()
Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
_orig = values
Return values.OfType(Of String).FirstOrDefault()
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetTypes() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
Return _orig
End Function
Public Overrides Function ProvideValue(ByVal serviceProvider As System.IServiceProvider) As Object
Return New LqvConfigurationMultiBindingConverter()
End Function
End Class
End Namespace
但它不起作用。你能帮助我吗?