20

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;      
    }   
}
Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36
Matthew
  • 3,976
  • 15
  • 66
  • 130
  • 2
    http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter(v=vs.110).aspx – Steve Nov 30 '13 at 00:42
  • 1
    The BooleanToVisibilityConberter seems good. I have a very similar implementation of bool to Visibility converter and its working fine, no error. I think the error source is somewhere else.. – har07 Nov 30 '13 at 03:39
  • 1
    just tried your converter code above, and it also works for me. Try to remove the converter and see if the error still there? only to confirm it caused the error. – har07 Nov 30 '13 at 03:58
  • Sometimes with Converters, rebuilding the project may work. just try !! – nkchandra Nov 30 '13 at 10:37
  • I did actually restart and rebuild the project and now there are no errors, although the actual visibility of the TextBlock is not showing/hiding. – Matthew Nov 30 '13 at 17:23
  • Breakpoints in Convert are hit? – Vitor Canova Nov 30 '13 at 17:40
  • Can't find declaration of IsTextBlockVisible. It is a DependencyProperty or has all INotifyPropertyChanged things in place? – Vitor Canova Nov 30 '13 at 17:43

3 Answers3

10

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();
    }


}
Developer
  • 4,158
  • 5
  • 34
  • 66
10

Here is mine:

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

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return value is Visibility && (Visibility)value == Visibility.Visible; 
    }
}
Miquel
  • 8,339
  • 11
  • 59
  • 82
5

There is already an implemenation of the converter: http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter(v=vs.110).aspx

Florian
  • 5,918
  • 3
  • 47
  • 86