将bool绑定到可见性属性

时间:2017-03-29 16:32:31

标签: c# data-binding uwp

我经常搜索这个问题。但是,似乎没有任何解决方案对我有用。我没有得到任何错误,但我已经为我的IValueConverter添加了一个断点。断点不会被触发。为什么不使用我的转换器?我想要做的就是使用视图模型策略来实现UI元素的可见性绑定(在本例中为复选框)。任何帮助表示赞赏。

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

视图模型:

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

    }
}

2 个答案:

答案 0 :(得分:1)

试试这个

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

现在应该可以了。

但我更喜欢BindableBase方法(在新课程中保存以下内容,并将其称为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));
            }
        }
    }
}

然后您的ViewModel将缩减为

using Mvvm;

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

还有最后一件事:在MainPage.xaml中要绑定到rootPivot,请致电

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

或类似的东西。

答案 1 :(得分:0)

您也可以尝试使用外部转换器库。 我找到了这个nuget包:click here

这个库使用起来非常简单,首先安装nuget包并插入:

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

作为视图顶部的命名空间,您现在可以将布尔值绑定到可见性

<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"/>

要查看工作示例,请访问github页面here