locked
Data Binding on IsEnabled RRS feed

  • Question

  • User386888 posted

    Hi, I am quite new to Xamarin and I am developing a BLE app. I want certain options in my MasterDetailPage menu only to be enabled when a device is connected through BLE. I am thinking I would have to bind the IsEnabled properties of the labels that I don't want to be enabled to the status of the BLE, but I am not sure how to do this. Can anyone help me out here? Thanks in advance, Sophie

    Friday, August 16, 2019 1:53 AM

Answers

  • User379860 posted

    @SophieHer First of all ,we can create a viewmodel, create a property for IsEnabled for label.

        public class MyViewModel: INotifyPropertyChanged
    {
        bool _changed = false;
        public bool Changed
        {
            get
            {
                return _changed;
            }
    
            set
            {
                if (_changed != value)
                {
                    _changed = value;
                    OnPropertyChanged("Changed");
    
                }
            }
    
        }
    
    
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    Then we bind the property in xaml.

         <Label 
            x:Name="mylabel"
           Text="Welcome to Xamarin.Forms!" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand"  IsEnabled="{Binding Changed, Mode=TwoWay}"/>
    

    In the end, we can change the isEnable for BLE state in the background code.

          public partial class MainPage : ContentPage
    {
        BluetoothState mystate;
        MyViewModel myViewModel;
        IBluetoothLE ble;
        public MainPage()
        {
            InitializeComponent();
    
             myViewModel =new MyViewModel();
            BindingContext = myViewModel;
            ble = CrossBluetoothLE.Current;
    
            var adapter = CrossBluetoothLE.Current.Adapter;
    
            ble.StateChanged += Ble_StateChanged;
    
    
    
    
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
    
            mystate = ble.State;
            if (mystate.Equals(BluetoothState.Off))
            {
                myViewModel.Changed = false;
    
            }
            else if (mystate.Equals(BluetoothState.On))
            {
    
                myViewModel.Changed = true;
            }
        }
        private void Ble_StateChanged(object sender, Plugin.BLE.Abstractions.EventArgs.BluetoothStateChangedArgs e)
        {
            mystate = ble.State;
            if (mystate.Equals(BluetoothState.TurningOff))
            {
                myViewModel.Changed = false;
    
            }
            else if (mystate.Equals(BluetoothState.TurningOn))
            {
    
                myViewModel.Changed = true;
            }
        }
    }
    

    Here is running GIF.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, August 16, 2019 7:32 AM

All replies

  • User379860 posted

    @SophieHer First of all ,we can create a viewmodel, create a property for IsEnabled for label.

        public class MyViewModel: INotifyPropertyChanged
    {
        bool _changed = false;
        public bool Changed
        {
            get
            {
                return _changed;
            }
    
            set
            {
                if (_changed != value)
                {
                    _changed = value;
                    OnPropertyChanged("Changed");
    
                }
            }
    
        }
    
    
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    Then we bind the property in xaml.

         <Label 
            x:Name="mylabel"
           Text="Welcome to Xamarin.Forms!" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand"  IsEnabled="{Binding Changed, Mode=TwoWay}"/>
    

    In the end, we can change the isEnable for BLE state in the background code.

          public partial class MainPage : ContentPage
    {
        BluetoothState mystate;
        MyViewModel myViewModel;
        IBluetoothLE ble;
        public MainPage()
        {
            InitializeComponent();
    
             myViewModel =new MyViewModel();
            BindingContext = myViewModel;
            ble = CrossBluetoothLE.Current;
    
            var adapter = CrossBluetoothLE.Current.Adapter;
    
            ble.StateChanged += Ble_StateChanged;
    
    
    
    
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
    
            mystate = ble.State;
            if (mystate.Equals(BluetoothState.Off))
            {
                myViewModel.Changed = false;
    
            }
            else if (mystate.Equals(BluetoothState.On))
            {
    
                myViewModel.Changed = true;
            }
        }
        private void Ble_StateChanged(object sender, Plugin.BLE.Abstractions.EventArgs.BluetoothStateChangedArgs e)
        {
            mystate = ble.State;
            if (mystate.Equals(BluetoothState.TurningOff))
            {
                myViewModel.Changed = false;
    
            }
            else if (mystate.Equals(BluetoothState.TurningOn))
            {
    
                myViewModel.Changed = true;
            }
        }
    }
    

    Here is running GIF.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, August 16, 2019 7:32 AM
  • User386888 posted

    @LeonLu Thank you so much for such an detailed answer! Works perfectly!!

    Friday, August 16, 2019 11:04 AM
  • User379860 posted

    @SophieHer You are welcome.

    Friday, August 16, 2019 3:01 PM