Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 535
Mishael Ogochukwu Avatar asked Dec 27 '16 14:12

Mishael Ogochukwu


1 Answers

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 ...}.

like image 66
Henk Holterman Avatar answered Sep 28 '22 09:09

Henk Holterman