Details about using MultiConverter to control the Button status in WPF

Source: Internet
Author: User

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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.