Xamarin Forms - 否定bool绑定值

Grz*_* G. 19 c# mvvm xamarin xamarin.forms

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

我有,让我们说使用isVisible Binding进入:

<Entry
    x:Name="TextEntry"
    IsVisible="{Binding IsVisibleEntry}"
/>
Run Code Online (Sandbox Code Playgroud)

并且Label我想隐藏时TextEntry可见.

<Label x:Name="MainLabel" 
       isVisible="!{Binding IsVisibleEntry}"/> //ofc it is not working
Run Code Online (Sandbox Code Playgroud)

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

Ziy*_*dil 40

选项一:转换器

public class InverseBoolConverter : IValueConverter, IMarkupExtension
    {
        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();
        }


        public object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }
Run Code Online (Sandbox Code Playgroud)

XAML

<Label x:Name="MainLabel" 
       isVisible="{Binding IsVisibleEntry, Converter={Helpers:InverseBoolConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

XAML标题

xmlns:Helpers="clr-namespace:HikePOS.Helpers"
Run Code Online (Sandbox Code Playgroud)

选项二:触发

<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>
Run Code Online (Sandbox Code Playgroud)

  • 这是一个旧的答案,但 ConvertBack 的主体中也应该有 `return !((bool)value);` 。 (2认同)

小智 18

您可以使用Xamarin Community Toolkit,而不是编写自己的转换器,该工具包现在有一个可以开箱即用的转换器invertedboolconverter 。这个例子(取自微软的文档)展示了它是如何工作的:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="MyLittleApp.MainPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <xct:InvertedBoolConverter x:Key="InvertedBoolConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout>

        <Label IsVisible="{Binding MyBooleanValue, Converter={StaticResource InvertedBoolConverter}}" />

    </StackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)


小智 7

您需要创建一个Invert转换器,以便您的绑定看起来像这样:

public class InverseBoolConverter : IValueConverter
{
    public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return !(bool)value;
    }
    public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return !(bool)value;
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的XAML中

<local:InverseBoolConverter x:Key="inverter"/>
<Entry
x:Name="TextEntry"
IsVisible="{Binding IsVisibleEntry, Converter={StaticResource inverter}}"
/>
Run Code Online (Sandbox Code Playgroud)