Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • WPF Converter : Show autoincrement number in ListBox

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 569
    Comment on it

    Introduction

    If you want to do data binding between  incompatible types, you need to use a converter. A converter converts the value from source type  to target type and vice versa. A value converter is a class, that implements the simple interface IValueConverter with the two methods
    1) object Convert(object value)
    2) object ConvertBack(object value).


    Implementing a ValueConverter :

    Add a class to the project and and  make it public and implement the IValueConverter interface like below :

    public class Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, 
            object parameter, CultureInfo culture)
        {
            // Do the conversion from bool to visibility
        }
     
        public object ConvertBack(object value, Type targetType, 
            object parameter, CultureInfo culture)
        {
            // Do the conversion from visibility to bool
        }
    }

     


    Implementing a ValueConverter in XAML

    First we need to  map the namespace of the converter to a XAML namespace. Then we  create an instance of a value converter in the resources of the view and give it a name.To reference the converter we use  {StaticResource}

     

    
    <Window x:Class="VirtualControlDemo.Window1"
        ...
        xmlns:l="clr-namespace:VirtualControlDemo"
        ...>
        <Window.Resources>
            <l:BoolToVisibilityConverter x:Key="converter" />
        </Window.Resources>
        <Grid>
            <Button Visibility="{Binding HasFunction, 
                Converter={StaticResource converter}}" />
        </Grid>
    </Window>

     

    Many times in WPF applications we need to show an auto increment counter in the begining of items in ListBox: Following is a converter to implement the same:

     public class IndexConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                try
                {
                    ListBoxItem item = value as ListBoxItem;
                    ListBox view = ItemsControl.ItemsControlFromItemContainer(item) as ListBox;
                    int index = view.ItemContainerGenerator.IndexFromContainer(item);
                    return (index + 1).ToString() + ". ";
                }
                catch (Exception)
                {
                    throw;
                }
    
            }
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    Hope the blog helped in understanding converters better.  Converters are powerful constructs used extensively in WPF.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: