WP7dev

Windows Phone 7 development tips

Category Archives: Converters

Empty List Visibility Converter

Hiding a Listbox or any item bound to a List when this List is empty (or null) is very common. You can use this simple converter to achieve this easily, what it does is casting the List to a generic ICollection, then uses ICollection.Count to check if the list is empty. Please note that this converter use is not limited to list, anything derived from ICollection can be used with it.

EmptyListVisibilityConverter.cs :


public class EmptyListVisibilityConverter : IValueConverter

{

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

    {

        if (value == null)

            return Visibility.Collapsed;

        else

        {

            ICollection list = value as ICollection;

            if (list != null)

            {

                if (list.Count == 0)

                    return Visibility.Collapsed;

                else

                    return Visibility.Visible;

            }

            else

                return Visibility.Visible;

        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    {

        throw new NotImplementedException();

    }

}


And here’s a simple example of how to use it :


<ListBox x:Name=”NameChoiceListBox”

         ItemsSource=”{Binding NamesList}”

         SelectedItem=”{Binding SelectedName, Mode=TwoWay}”

         ItemTemplate=”{StaticResource DataTemplateNameChoice}”

         Visibility=”{Binding NamesList, Converter={StaticResource EmptyListVisibilityConverter}}”>


Advertisements

Null Visibility Converter

Needing to hide an object from a view when its binded data is empty is something quite common in WP7 development, to achieve this, a very simple way is to use this converter :

NullVisibilityConverter :


public class NullVisibilityConverter : IValueConverter

{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    {

        return value == null ? Visibility.Collapsed : Visibility.Visible;

    }

 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    {

        Visibility? v = value as Visibility?;

        return ((v.HasValue) || (v.Value == Visibility.Collapsed)) ? null : “”;

    }

}


Using this, you can bind your object Visibility to the value you want to check, and use the converter to hide it when it’s null :


<TextBlock Style=”{StaticResource ErrorMessageStyle}”

           Text=”{Binding CurrentErrorMessage}”

           Visibility=”{Binding CurrentErrorMessage, Converter={StaticResource

           NullVisibilityConverter}}”/>


This converter can also be adapted for Listbox to check whether its ItemsSource (List or ObservableCollection) is null or empty in order to hide the listbox and display something else

Boolean Inverter Converter

Basic value converter, but very useful when using linked CheckBox or RadioButton :


    public class BoolInverterConverter : IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter,

                              System.Globalization.CultureInfo culture)

        {

            return !(bool)value;

        }

 

        public object ConvertBack(object value, Type targetType, object parameter,

                                  System.Globalization.CultureInfo culture)

        {

            return !(bool)value;

        }

    }


Example usage : 2 Checkboxes linked to the same value, one is inverted (Value “No”). This way, when checking one box, the other gets unchecked.


<StackPanel Orientation=”Horizontal” VerticalAlignment=”Top”>

<CheckBox Style=”{StaticResource RadioButtonStyle}”

                 IsChecked=”{Binding Tested, Mode=TwoWay}”>

             <TextBlock Margin=”10,0,0,0″ FontWeight=”Bold” FontSize=”24″

Text=”{Binding StringLibrary.Tested_Yes, Source={StaticResource LocalizedStrings}}”/>

       </CheckBox>

       <CheckBox Style=”{StaticResource RadioButtonStyle}”

                 IsChecked=”{Binding Tested, Mode=TwoWay,

                            Converter={StaticResource BoolInverterConverter}}”>

             <TextBlock Margin=”10,0,0,0″ FontWeight=”Bold” FontSize=”24″

Text=”{Binding StringLibrary.Tested_No, Source={StaticResource LocalizedStrings}}”/>

       </CheckBox>

</StackPanel>