Xamarin Forms - 否定 bool 绑定值 [英] Xamarin Forms - negate bool binding values

查看:26
本文介绍了Xamarin Forms - 否定 bool 绑定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 xamarin 形式和 mvvm 模式.我想知道,是否有可能否定绑定 bool 值.我的意思是:

I am learning the xamarin forms and mvvm pattern. I am wondering, if is it possible to negate binding bool value. What I mean is:

我有,比方说带有 isVisible Binding 的条目:

I have, let's say Entry with isVisible Binding:

<Entry
    x:Name="TextEntry"
    IsVisible="{Binding IsVisibleEntry}"
/>

TextEntry 可见时,我想隐藏

Label.

and Label which i want to hide when TextEntry is visible.

<Label x:Name="MainLabel" 
       isVisible="!{Binding IsVisibleEntry}"/> //ofc it is not working

是否可以不为 ViewModel 中的 MainLabel 创建新变量?

Is it possible to do without making a new variable for MainLabel in ViewModel?

推荐答案

选项一:转换器

定义转换器:

    public class InverseBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return !((bool)value);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
            //throw new NotImplementedException();
        }
    }

在 XAML 中的使用:

    <Label x:Name="MainLabel" 
           isVisible="{Binding IsVisibleEntry, Converter={Helpers:InverseBoolConverter}}"/>

XAML 标头

    xmlns:Helpers="clr-namespace:HikePOS.Helpers"

选项二:触发

    <Label x:Name="MainLabel" isVisible="{Binding IsVisibleEntry}">
        <Label.Triggers>
            <DataTrigger TargetType="Label" Binding="{Binding IsVisibleEntry}" Value="True">
                <Setter Property="IsVisible" Value="False" />
            </DataTrigger>
        </Label.Triggers>
    </Label>

这篇关于Xamarin Forms - 否定 bool 绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆