Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Visibility with x:Bind not updating with MVVM

Since the Anniversary Update (Build 14383 / 14393) you should be able to toggle the visibility of XAML elements without using a converter, like this:

<TextBlock Text="I'm not visible!" Visibility="{x:Bind IsVisibleFalse}" />
<TextBlock Text="I'm visible!" Visibility="{x:Bind IsVisibleTrue}" />

I was trying this in my project, minimum target version set to Windows 10 Anniversary Edition. Unfortunately I did not get it to work.

This code works just fine:

<StackPanel Visibility="{x:Bind ViewModel.IsUnlocked, 
    Converter={StaticResource BoolToVisibilityConverter}, Mode=TwoWay}">

This one doesn't (no error on compile, just does not show up when the bool value changes):

<StackPanel Visibility="{x:Bind ViewModel.IsUnlocked}>

I suspect the Mode="TwoWay" to be the issue, as you can't set it "when the binding expression ends with a cast". This code does not work as well:

<StackPanel Visibility="{x:Bind ViewModel.IsUnlocked,
   Converter={StaticResource BoolToVisibilityConverter}>

So my question is: Am I misssing something or is this not working yet in a MVVM-Scenario and only with code-behind?

like image 655
Thomas Avatar asked Feb 01 '26 01:02

Thomas


1 Answers

The default Mode is OneTime, which renders your code not working. I suggest you use OneWay, which should be usable upon casting.

like image 187
Dante May Code Avatar answered Feb 03 '26 15:02

Dante May Code



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!