我正在学习xamarin表单和mvvm模式。我想知道,是否有可能否定绑定布尔值。我的意思是:
我有,让我们说使用isVisible绑定的Entry:
<Entry
x:Name="TextEntry"
IsVisible="{Binding IsVisibleEntry}"
/>
字符串
和Label
,当TextEntry
可见时,我想隐藏它。
<Label x:Name="MainLabel"
isVisible="!{Binding IsVisibleEntry}"/> //ofc it is not working
型
是否可以在ViewModel中不为MainLabel创建新变量?
5条答案
按热度按时间shyt4zoc1#
方案一:转换器
定义转换器:
字符串
XAML用法:
型
XAML Header
型
方案二:触发器
型
t9eec4r02#
您可以使用Xamarin Community Toolkit,而不是编写自己的转换器,它现在有一个可以开箱即用的转换器invertedboolconverter。这个例子(摘自微软的文档)展示了它是如何工作的:
字符串
8qgya5xd3#
你需要创建一个Invert转换器,这样你的绑定看起来就像这样:
字符串
在XAML中
型
bwleehnv4#
尽管@praty的解决方案有效,但如果你像我一样使用自定义控件/视图或XCT的弹出窗口。在XAML中使用转换器之前,我们需要像这样指定:
字符串
因此,最终代码将是:
InverseBoolConverter.cs
型
XAML代码
型
**注意:**虽然我们可以使用Xamarin Community Toolkit的InvertedBoolConverter,但我已经描述了手动解决方案,以便我们可以添加自定义值转换器而不是内置转换器。
pw136qt25#
我喜欢已经提供的答案,但我想我会添加我自己的版本,我一直在使用有时。在MVVM体系结构中,视图模型的属性都在对象设置器中调用
SetProperty()
,这会引发INotifyPropertyChanged
事件。我所做的是向视图模型添加第二个属性,并在第一个属性的setter中将其值设置为!value
。例如,只要属性
ViewModel.IsBusy = true
是这种情况,我就希望按钮是button.IsEnabled = false
。这是一个反向的bool例子,就像你问的那样。ViewModel.cs属性:
字符串
XAML:
型
这样,在我的代码中,每当我调用
IsBusy = true
时,它都会同时设置button.IsEnabled = false
,解决了反向bool问题。