Valueconverter is used in WPF to implement "range condition trigger"

Source: Internet
Author: User

In WPF, we know that the interface layer can implement the "condition"-"assign value" function through the trigger.

Attribute trigger property trigger: triggered when the value of dependency property changes.
Data trigger: triggered when the value of the common. Net attribute changes.
Event trigger: called when the route time is triggered.

Attribute triggers and data triggers can have multiple condition triggers, which are similar to the effect of "Parallel" in condition statements.

However, in trigger application scenarios, the limit of "condition" is "equal to", that is, a variable triggers a "value assignment" at a specific value"

Trigger does not seem suitable if you want to reach the range of "greater than" less"

A simple and common requirement in a project. When a data value is smaller than or equal to 0, it is hidden or blank.

The implementation of valueconverter is attempted, and the principle is very simple. That is, in the Custom valueconverter class, you can specify the "data processing process"

This data processing process can be a simple conversion based on a specific number, for example, 1 to "Monday", 2 to "Tuesday"

It can also be a condition or business judgment. As mentioned above, when the raw data is less than or equal to 0, it is converted to string. Empty

It can even be complicated business logic conversion.

In this way, a Range condition trigger can be implemented.

Interface code

<TextBlock Text="{Binding Path=MyData,Converter={StaticResource dataConverter}}"/>

Interface Resource Declaration

<local:DataConverter x:Key="dataConvert"/>

 

Valueconverter code

[ValueConversion(typeof(string),typeof(string))]public class DataConverter:IValueConverter{public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture){int tempValue=0;int.TryParse((string)value,out tempValue);if(tempValue<=0){return string.empty;}else{return (string)value;}}public object ConvertBack(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture){return (string)value;}}

 

Valueconverter is used in WPF to implement "range condition trigger"

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.