Click here to check my latest exciting videos on youtube
Search Mallstuffs

Flag Counter
Spirituality, Knowledge and Entertainment

Locations of visitors to this page

Latest Articles

Move to top
Hide or unhide data using Visibility ValueConverter class
Posted By Sarin on May 02, 2013     RSS Feeds     Latest Hinduism news

Why We Need Value Converters
Data from database comes in many different ways and most of the times; you need to format the data before it is bound to the control. In silver light, we use value converter to format the data before it is bounded to the control. Simplest example of using value converter would be to format the date to be displayed in “date of birth” column.
How to use Value Converters
Using a value converter is a very simple process in Silverlight. To create your own value converter in silver light, you need to create a class that inherits the IValueConverter interface. To use this class, you need to add a reference to System.Windows.Data namespace. This class mandatorily needs to define two functions, Convert and ConvertBack. Convert will format your data before binding it to the control. ConvertBack does exactly the opposite. This function allows the user to convert the modified data back to its original form. To automatically declare these two functions, just click on the class name (Deriving IValueConverter) and select Implement Interface from the context menu to let the visual studio declare the two methods.  
Both functions have same parameters. First is the data to be bound, type of data, optional parameters to control how data should be converted and last is the target culture (English, Hindi, French etc).  
Here's what the method signatures look like:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

To use ValueConverter, first we need to declare the namespace under which we have defined our ValueConverter class.


After declaring namespace, you need to add a reference to your class as  
            <local:VisibilityConverter x:Name="VisConverter"/>

 ‘VisibilityConverter’ is the name of ValueConverter class. VisConverter will act as a key to force data to pass through my ValueConverter class before it is bounded to the control. I have used it in my DataGridTemplateColumn column as  
    <sdk:DataGridTemplateColumn Header="Age" Width="80">
           <TextBlock Text="Binding Age” Visibility="

As you see above, I have used key ‘VisConverter’ in the visibility attribute of text block. Value (DisplayAge) passed is of Boolean type. This value is used in my simple ValueConverter class as

    public class VisibilityConverter : IValueConverter
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            bool display;
            if (bool.TryParse(value.ToString(), out display) && display)
                return Visibility.Visible;
                return Visibility.Collapsed;
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            return null;

As you see above, DisplayAge will be passed as the first parameter to convert class. Then, I have just parsed the value to check if it is Boolean and then accordingly, hide or unhide the age onto the datagrid.
If I run my code, then I can see output as

Hide or unhide data using Visibility ValueConverter class
 As shown above, age is not displayed for records whose ‘DisplayAge’ property was set to false.

Thus, you have seen how to manipulate data as it passes in and out of the bounded control. Using value converter, you can write your own pretty clean custom logic to change the data to be displayed on your datagrid. Happy coding.

Note: Images used on this website are either a production of Bhaktivedanta Book Trust(, Iskcon Foundation or were found in google search under "Free to use and share". If any of the images presented here violates copyright issues or infringes anyone copyright or are not under "Fair use", then please bring it to our notice. Read Disclaimer for more.

Share this to your friends. One of your friend is waiting for your share.
Related Articles
How music affects plant growth
Working with silver light datagrid
Hide or unhide data using Visibility ValueConverter class
Tips and tricks with ajax calendar extender control

Post Comment