简体   繁体   中英

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). I bound the visibility property of the control in my view to a bool property in my view model and surprisingly it works. 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

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.

I just removed a BooleanToVisibility conversion from a {x:Bind ...} in my project and indeed it still works. 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. And you do need to target 14393 or later. For older builds it only works in {Binding ...} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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