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"