c# – 如何实现BoolToVisibilityConverter

前端之家收集整理的这篇文章主要介绍了c# – 如何实现BoolToVisibilityConverter前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的应用程序中,我想在StackPanel中切换项目的可见性.我的Stackpanel包含一个图像和一个TextBlock.如何正确使用BoolToVisibilityConverter来切换TextBlock的可见性,并为用户保存此设置?

目前我所拥有的如下,虽然我有几个错误.重要说明,我需要使用一个ApplicationBar菜单项作为驱动切换TextBox可视性的点击事件.

编辑

尽管TextBlock的可见性没有改变,但错误不再发生.

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>

代码背后

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
        }               
    }

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,CultureInfo language)      
    {      
        return value is Visibility && (Visibility)value == Visibility.Visible;      
    }   
}

解决方法

尝试这个:
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,string language)
    {
        return GetVisibility(value);
    }
    public object ConvertBack(object value,string language)
    {
        throw new NotImplementedException();
    }


}

猜你在找的C#相关文章