简体   繁体   中英

Binding bool to visibility in code behind

I want to change the visibility of the TextBlock with a Binding depending on the state of the boolean variable of the code behind

<TextBlock Text="Hello">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsVisible}" Value="True">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

This don't work:(

    private bool _isVisible = false;
    public bool IsVisible
    {
        get { return _isVisible; }
        set
        {
            _isVisible = value;
            NotifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

The problem is within your propertyname IsVisible . You are in the CodeBehind of a Window or a UserControl , so there is already a property called IsVisible which comes from the Window / UserControl .

Just change the name of your property to something like IsCurrentlyVisible and your code will work.

If you have more places in your code where you have to set the Visibility of a control dependent on a bool-property I would recommend you to use a converter instead of the style.

The converter-class for converting a bool-value to a Visibility looks like:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace Playground
{
    internal class BoolToVisibilityConverter : MarkupExtension, IValueConverter
    {
        private static BoolToVisibilityConverter converter;

        // ReSharper disable once EmptyConstructor
        public BoolToVisibilityConverter()
        {

        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return converter ?? (converter = new BoolToVisibilityConverter());
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool b && b)
                return Visibility.Visible;
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

And the usage of this converter is just as easy as:

<TextBlock Text="Hello" Visibility="{Binding IsCurrentlyVisible, Converter={local:BoolToVisibilityConverter}}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM