Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implement a BoolToVisibilityConverter

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;      
    }   
}
like image 404
Matthew Avatar asked Nov 30 '13 00:11

Matthew


3 Answers

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


}
like image 106
Cheese Avatar answered Nov 06 '22 14:11

Cheese


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; 
    }
}
like image 9
Miquel Avatar answered Nov 06 '22 13:11

Miquel


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

like image 5
Florian Avatar answered Nov 06 '22 15:11

Florian