简体   繁体   English

为什么bool在使用传统绑定时没有转换器就可以实现可视性

[英]Why does bool work for visibility without a converter when using traditional binding

I have created and used bool to visibility converters before and the other day, I forgot to use a converter on the binding (I am using traditional binding). 我之前和之前创建并使用bool到可见性转换器,我忘了在绑定上使用转换器(我正在使用传统绑定)。 I bound the visibility property of the control in my view to a bool property in my view model and surprisingly it works. 我将视图中控件的visibility属性绑定到视图模型中的bool属性,令人惊讶的是它可以工作。 So my question is if it works with traditional binding, why do we need to use converters? 所以我的问题是,如果它适用于传统绑定,为什么我们需要使用转换器? Because it seems the compiler is doing the conversion for me. 因为看起来编译器正在为我做转换。

I tested it on a UWP app in Visual studio Update 3. The minimum app target is 10.0.10586 The target version is 10.0.14393 我在Visual Studio Update 3中的UWP应用程序上测试了它。最小应用程序目标是10.0.10586目标版本是10.0.14393

Interesting. 有趣。 This has always been a pain and it seems to have been fixed without much publicity, I didn't know this. 这一直是一种痛苦,似乎没有太多宣传就修好了,我不知道这一点。

In WPF you always had to use a ValueConverter, because Visibility isn't a bool. 在WPF中,您总是必须使用ValueConverter,因为Visibility不是bool。

I just removed a BooleanToVisibility conversion from a {x:Bind ...} in my project and indeed it still works. 我刚从项目中的{x:Bind ...}中删除了一个BooleanToVisibility转换,实际上它仍然有效。 I dug this up from the generated code: 我从生成的代码中挖出了这个:

private void Update_ViewModel_ShowMessage(global::System.Boolean obj, int phase)            
{
  ...
  this.Update_ViewModel_ShowMessage_Cast_ShowMessage_To_Visibility(
    obj ? global::Windows.UI.Xaml.Visibility.Visible 
        : global::Windows.UI.Xaml.Visibility.Collapsed
   , phase);
...
}

So apparently it is now built in. 显然它现在已经内置了。

Update: 更新:

For {x:Bind } it was announced here , as part of the anniversary update. 对于{x:Bind }它在这里宣布,作为周年纪念更新的一部分。 And you do need to target 14393 or later. 而你确实需要针对14393或更高版本。 For older builds it only works in {Binding ...} . 对于旧版本,它仅适用于{Binding ...}

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

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