简体   繁体   中英

Bind bool to visibility property

I've searched this question a lot. But, it seems no solutions are working for me. I don't get any errors but I've added a breakpoint to my IValueConverter. The breakpoint does not ever get triggered. Why is it not using my converter? All I want to do is use view model strategy for visibility binding of a UI element (in this case a checkbox). Any help is appreciated.

IValueConverter:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace Test_Tool.Common
{
    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language) => (bool)value ^ (parameter as string ?? string.Empty).Equals("Reverse") ? Visibility.Visible : Visibility.Collapsed;

        public object ConvertBack(object value, Type targetType, object parameter, string language) => (Visibility)value == Visibility.Visible ^ (parameter as string ?? string.Empty).Equals("Reverse");
    }
}

XAML:

<Page
    x:Class="Test_Tool.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Test_Tool"
    xmlns:converter="using:Test_Tool.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.Resources>
            <converter:BooleanToVisibilityConverter x:Key="cvt" />
        </Grid.Resources>
        <Pivot x:Name="rootPivot" Title="Test Tool" >
            <PivotItem Header="Test Selection">
                <StackPanel>
                    <CheckBox x:Name="dppCheckBox" Content="DPP" Margin="5,8,5,5" Visibility="{Binding IsDirect, Converter={StaticResource cvt}}" />
                </StackPanel>
            </PivotItem>
        </Pivot>
    </Grid>
</Page>

ViewModel:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Test_Tool.ViewModels
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
        //Localized private vars
        private bool _isDirect;

        //Public vars for bindings
        public bool IsDirect
        {
            get
            {
                return _isDirect;
            }
            set
            {
                _isDirect = value;
                OnPropertyChanged();
            }
        }

        public MainPageViewModel()
        {
            //Any Initialization
            IsDirect = false;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged([CallerMemberName]string propertyName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

MainPage:

using DM_API_Test_Tool.ViewModels;
using Windows.UI.Xaml.Controls;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace Test_Tool
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPageViewModel ViewModel { get; set; }
        public MainPage()
        {
            this.InitializeComponent();
            this.ViewModel = new MainPageViewModel();
        }

    }
}

Try this

namespace Test_Tool.ViewModels
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
        private bool _isDirect = false;
        public bool IsDirect
        {
            get
            {
                return _isDirect;
            }
            set
            {
                set { SetField(ref _isDirect, value, "isDirect"); }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        protected bool SetField<T>(ref T field, T value, string propertyName)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }
    }
}

this should work now.

However I prefer the BindableBase method more (save the content below in a new class and call it something like BindableBase.cs )

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Mvvm
{
    public abstract class BindableBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
        {
            if (object.Equals(storage, value)) return false;

            storage = value;
            this.OnPropertyChanged(propertyName);
            return true;
        }

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var eventHandler = this.PropertyChanged;
            if (eventHandler != null)
            {
                eventHandler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

and then your ViewModel would be reduced to

using Mvvm;

namespace Test_Tool
{
    public class MainPageViewModel : BindableBase
    {
        private bool _isDirect = false;
        public bool IsDirect
        {
            get { return _isDirect; }
            set { SetProperty(ref _isDirect, value); }
        }
    }
}

and one last thing: In your MainPage.xaml you want to bind to rootPivot , so you call

rootPivot.DataContext = null;
rootPivot.DataContext = new MainPageViewModel();

or something like that.

You can also try using external convertor library. I found this nuget package: click here

This library is very simple to use, first install the nuget package and insert:

<Window x:Class="WPFUI.Views.MainWindow"
    ...
    xmlns:convertor="clr-namespace:suren37.convertor;assembly=suren37.convertor"
    ...>

as namespace at the top of the view and you can now bind a boolean value to the visibility as

<CheckBox Grid.Row="1" IsChecked="{Binding IsVisibile}" Grid.Column="1" Margin="8 0" />
<TextBlock Grid.Row="1" Text="This text becomes visible on checked"
                   Visibility="{Binding IsVisibile, Converter={convertor:BoolToVisibilityConverter}}"
                   Grid.Column="2" TextWrapping="WrapWithOverflow"/>

To check out the working sample visit the github page, here .

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