Requirement Description
1. The button status must be determined based on the content of multiple data sources.
2. Custom judgment rules for each data source
Note: The following features are simple and crude. If you know more about the elegant solution, please let me know. Thank you!
Button XAML
Copy codeThe Code is as follows:
<Button Name = "btnOK"
Grid. Column = "2"
HorizontalAlignment = "Left"
VerticalAlignment = "Center"
Command = "{Binding Path = OKCommand }"
Content = "{DynamicResource Common_Button_ OK }"
IsDefault = "True"
Style = "{DynamicResource ButtonStyle}">
<Button. IsEnabled>
<MultiBinding Converter = "{StaticResource InvalidMultiValidationRuleToBooleanMultiConverter }"
ConverterParameter = "objectnull | greaterthanzerointeger">
<Binding ElementName = "comboBoxFilter"
Mode = "OneWay"
Path = "SelectedItem"/>
<Binding ElementName = "textBoxFrameRate"
Mode = "OneWay"
Path = "Text"/>
<Binding ElementName = "textBoxSizeWidth"
Mode = "OneWay"
Path = "Text"/>
<Binding ElementName = "textBoxSizeHeight"
Mode = "OneWay"
Path = "Text"/>
</MultiBinding>
</Button. IsEnabled>
</Button>
MultiConverter judgment
Copy codeThe Code is as follows:
Public class InvalidMultiValidationRuleToBooleanMultiConverter: IMultiValueConverter
{
# Region IMultiValueConverter Members
Public object Convert (object [] values, Type targetType, object parameter, CultureInfo culture)
{
String [] paramlist = (string) parameter). Split ('| ');
If (paramlist = null | paramlist. Length <= 0)
{
Throw new ArgumentNullException ("parameter ");
}
Int length = paramlist. Length;
IList <bool> boollist = new List <bool> ();
For (int I = 0; I <paramlist. Length; I ++)
{
Switch (paramlist [I]. ToLowerInvariant ())
{
Case "checknameexisted ":
Boollist. Add (ValidationRuleHelper. Validate <InvalidCheckNameExistedValidationRule> (values [I]);
Break;
Case "directoryandfileexist ":
Boollist. Add (ValidationRuleHelper. Validate <InvalidDirectoryAndFileExistValidationRule> (values [I]);
Break;
Case "greaterthanzerointeger ":
Boollist. Add (ValidationRuleHelper. Validate <InvalidGreaterThanZeroIntegerValidationRule> (values [I]);
Break;
Case "numericnull ":
Boollist. Add (ValidationRuleHelper. Validate <InvalidNumericNullValidationRule> (values [I]);
Break;
Case "stringlength ":
Boollist. Add (ValidationRuleHelper. Validate <InvalidStringLengthValidationRule> (values [I]);
Break;
Case "stringnullorempty ":
Boollist. Add (ValidationRuleHelper. Validate <InvalidStringNullOrEmptyValidationRule> (values [I]);
Break;
Case "ipaddress ":
Boollist. Add (ValidationRuleHelper. Validate <InvalidIPAddressValidationRule> (values [I]);
Break;
Case "objectnull ":
Default:
Boollist. Add (ValidationRuleHelper. Validate <InvalidObjectNullValidationRule> (values [I]);
Break;
}
}
Bool result = boollist [0];
For (int I = 1; I <boollist. Count; I ++)
{
Result = result & boollist [I];
}
Return result;
}
Public object [] ConvertBack (object value, Type [] targetTypes, object parameter, CultureInfo culture)
{
Return null;
}
# Endregion
}