How can I prevent flicker when binding Booleans to the visibility of a control

I have a boolean property in my ViewModel called let say IsNotSupported , which is used to display some warning information if the sensor is not supported. Therefore, I use the BooleanToVisibilityConverter , which is added to ressources:

 <phone:PhoneApplicationPage.Resources> <local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> </phone:PhoneApplicationPage.Resources> 

and bind it to the stack panel containing the warning:

 <StackPanel x:Name="NotSupportedWarning" Visibility="{Binding IsNotSupported, Converter={StaticResource BooleanToVisibilityConverter}}"> 

This works very well, but when the page loads and the sensor is supported, the warning appears for only a split second and then disappears. I know that this flicker is caused by the fact that the binding has not yet been completed, and therefore it is not visible by default.

This flicker, it is annoying as hell ... It should be the default by default, and it becomes visible only after it becomes clear that the warning should be shown. In addition, this avoids the second prototyping after binding and, therefore, can have a positive impact on performance.


I had this problem again and again, and I did not find anything about it on the Internet until I found this SO question, which is closely related, but is not found, if the search instead of Windows Phone instead of Windows. Both the problem and the solution may seem simple, but I really listened to me for quite a while, so I thought it would be nice to write a Q & A-style question about this to help others who are facing the same problem.

+4
source share
2 answers

The solution is simple after you see it. You can control the default value of the binding (if the binding has not yet FallbackValue place) with FallbackValue . Your XAML stack panel will look like this:

 <StackPanel x:Name="NotSupportedWarning" Visibility="{Binding IsNotSupported, FallbackValue=Collapsed, Converter={StaticResource BooleanToVisibilityConverter}}"> 

Thus, you will get rid of flicker and do not have to be relayed after binding if the warning remains hidden.

+4
source

you can bind directly to the type of the visibility property instead of the boolean and keep this default property by default, plus you can implement INotifyPropertyChanged

+1
source

All Articles