How to Implement a BoolToVisibilityConverter

Matthew picture Matthew · Nov 30, 2013 · Viewed 24.7k times · Source

In my app I would like to toggle the visibility of an item in a StackPanel. My Stackpanel contains an Image and a TextBlock. How would I properly use a BoolToVisibilityConverter to toggle the visibility of the TextBlock, and save this setting for the users benefit?

Currently what I have is as follows, although I am getting a few errors. Important note, I need to use an ApplicationBar menu item as the click event that drives the toggling of the TextBox visibility.

EDIT

Error no longer occurring although the visibility of the TextBlock is not changing.

XAML

xmlns:common="clr-namespace:TestApp.Common"

<phone:PhoneApplicationPage.Resources>
    <common:BooleanToVisibilityConverter x:Key="BoolToVisConv" />
</phone:PhoneApplicationPage.Resources>

<ListBox Name="ListBoxEffects" SelectionMode="Single" ItemsSource="{Binding}" Margin="{Binding}"
                     toolkit:TiltEffect.IsTiltEnabled="True" SelectionChanged="ListBox_SelectionChanged" 
                         ItemContainerStyle="{StaticResource ListBoxItemStyle1}">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <toolkit:WrapPanel ItemWidth="159" ItemHeight="Auto" />
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical"  >
                                <Image Source="{Binding Thumbnail}" Width="155" Height="155" />
                                <TextBlock Text="{Binding Name}" Visibility="{Binding IsTextBlockVisible, Converter={StaticResource BoolToVisConv}}"  TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeNormal}" VerticalAlignment="Center" HorizontalAlignment="Center" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

Code Behind

private void BuildLocalizedApplicationBar()
    {
        ApplicationBar = new ApplicationBar();

        ApplicationBarMenuItem showFilterNamesMenuItem = new ApplicationBarMenuItem();
        if (Settings.ShowFilterNames.Value)
            showFilterNamesMenuItem.Text = AppResources.EditPage_EffectNames_Hide;
        else
            showFilterNamesMenuItem.Text = AppResources.EditPage_EffectNames_Show;
        showFilterNamesMenuItem.Click += showFilterNamesMenuItem_Click;
        ApplicationBar.MenuItems.Add(showFilterNamesMenuItem);
    }

void showFilterNamesMenuItem_Click(object sender, EventArgs e)
    {
        if(Settings.ShowFilterNames.Value)
        {
            ((ApplicationBarMenuItem)ApplicationBar.MenuItems[0]).Text = AppResources.EditPage_EffectNames_Hide;
            Settings.ShowFilterNames.Value = false;

            //Toggle the text block visibility to here
        }
        else
        {
            ((ApplicationBarMenuItem)ApplicationBar.MenuItems[0]).Text = AppResources.EditPage_EffectNames_Show;
            Settings.ShowFilterNames.Value = true;

            //Toggle the text block visibility to here
        }               
    }

A class for the BooleanToVisibilityConverter

//Error on BooleanToVisibilityConverter stating does not implement interface member 'System.Windows.Data.IValueConverter.Convert(object, System.Type, object, System.Globalization.CultureInfo)
public class BooleanToVisibilityConverter : IValueConverter   
{   

public class BooleanToVisibilityConverter : IValueConverter   
{   
    public object Convert(object value, Type targetType, object parameter, CultureInfo language)      
    {      
        return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;      
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo language)      
    {      
        return value is Visibility && (Visibility)value == Visibility.Visible;      
    }   
}

Answer

Cheese picture Cheese · Dec 3, 2013

Try this:

public class BooleanToVisibilityConverter : IValueConverter
{
    private object GetVisibility(object value)
    {
        if (!(value is bool))
            return Visibility.Collapsed;
        bool objValue = (bool)value;
        if (objValue)
        {
            return Visibility.Visible;
        }
        return Visibility.Collapsed;
    }
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return GetVisibility(value);
    }
    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }


}