in Education by
What is value convertor in WPF?

1 Answer

0 votes
by
A Value Converter functions as a bridge between a target and a source and it is necessary when a target is bound with one source, for instance you have a text box and a button control. You want to enable or disable the button control when the text of the text box is filled or null.
 
In this case you need to convert the string data to Boolean. This is possible using a Value Converter. To implement Value Converters, there is the requirement to inherit from I Value Converter in the System.Windows.Data namespace and implement the two methods Convert and Convert Back.
 
Note: In WPF, Binding helps to flow the data between the two WPF objects. The bound object that emits the data is called the Source and the other (that accepts the data) is called the Target.
 
Example
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows.Data;  
  7. namespace ValueConverters  
  8. {  
  9.     public class ValueConverter: IValueConverter  
  10.     {  
  11.         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  12.         {  
  13.             bool isenable = true;  
  14.             if (string.IsNullOrEmpty(value.ToString()))  
  15.             {  
  16.                 isenable = false;  
  17.             }  
  18.             return isenable;  
  19.         }  
  20.         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  21.         {  
  22.             throw new NotImplementedException();  
  23.         }  
  24.     }  
  25. }  

Related questions

0 votes
    What are the WPF Content Controls?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
0 votes
0 votes
    What is WPF Dependency Property and how can we use?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
0 votes
0 votes
    What is Trigger and how many types of triggers in WPF?...
asked Apr 8, 2021 in Education by JackTerrance
...