简体   繁体   中英

How to bind SwitchCell text color to view model in Xamarin.Forms

I am trying to bind SwitchCell text color to view model, but SwitchCell.TextColor bindable property does not exist. How can I bind text color of switchCell ?

There some code:

var channelStatusSwitch = new SwitchCell {Text = AppResources.ChannelStatusSwitchText};
channelStatusSwitch.SetBinding(SwitchCell.OnProperty, new Binding("IsChannelRunning", BindingMode.TwoWay));
channelStatusSwitch.SetBinding(/*text color poperty*/, new Binding("IsChannelRunning", BindingMode.OneWay,new BoolToColorConverter()));

SwitchCell only exposes 2 bindable properties On and Text . If you need a text color property then you need to create a custom control.

SwitchCell is a Cell with a label and an on/off switch.

There you go, you can create a custom control in your shared or PCL project itself and you wont need platform specific custom renders to implement this.

You can do it in XAML itself or from code behind by creating a CustomSwitchCell inheritted from Cell and adding a Label and Switch to it. Then expose your TextColor property and use it to set the Label's Font Color.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM