IsNotBusy Binding?

IanVinkIanVink CAInsider, University ✭✭✭

I often find myself needing to set the visibility of a UI element to something like, "IsNotBusy" (Save Button should disappear when the page IsBusy for example)

   savebutton.SetBinding(IsVisibleProperty, (not)"IsBusy");

I have been creating a property called IsNotBusy and binding to that, but it feels clunky.

Is there an easier way to set the visibility to "not" IsBusy" ?

Posts

  • LuckyDayLuckyDay USMember ✭✭✭

    Your best alternatives are probably either a negation converter or using Triggers in Styles

  • IanVinkIanVink CAInsider, University ✭✭✭

    But of course....

    Here's what I use now:

    /// <summary>
    /// Inverts a bool, handy for IsBusy
    /// </summary>
    public class NotConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return false;
    
            var str = value.ToString();
    
            if (str.IsBoolean())
            {
                return !str.ToBool();
            }
            return false;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
  • adamkempadamkemp USInsider, Developer Group Leader mod

    Why convert to a string? My implementation is here: https://github.com/TheRealAdamKemp/Xamarin.Forms-Tests

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @IanVink‌ could you post a larger piece of code? I've not understand how use "Convert" with "Binding".
    Thanks

  • IanVinkIanVink CAInsider, University ✭✭✭
    edited January 2015

    @acaliaro, In your Page you'll have a View, like a Button

     var btnSave = new Button();
    

    To set it invisible when the Page.IsBusy is set to true, do this:

     btnSave.SetValue(IsVisibleProperty, new Binding("IsBusy", BindingMode.Default, new NotConverter()));
    

    Then in your page when you set IsBusy = true, the button will disappear.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @IanVink‌ , thanks, clear and very useful.

Sign In or Register to comment.