繁体   English   中英

将布尔值绑定到可见性属性

[英]Bind bool to visibility property

我已经搜索了很多这个问题。 但是,似乎没有解决方案对我有用。 我没有收到任何错误,但是我向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>

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

主页:

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

    }
}

尝试这个

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

或类似的东西。

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

这个库的使用非常简单,首先安装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页面, 此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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