Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Bindable property is not working on custom control

$
0
0

Hi everyone I want to make a custom control called switch label. I put label and switch inside the stack layout. My purpose is to make a control like SwitchCell.

<?xml version="1.0" encoding="UTF-8"?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MobileRKE.Controls.SwitchLabel"
             Orientation="Horizontal">
    <Label x:Name="lbTitle" HorizontalOptions="FillAndExpand" Text="{Binding Title}" VerticalOptions="Center"/>
    <Switch x:Name="swtValue"
            IsToggled="{Binding IsOn}"
            VerticalOptions="Start"/>
</StackLayout>

code behind

[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SwitchLabel : StackLayout
    {
        public SwitchLabel()
        {
            BindingContext = this;
            InitializeComponent();
        }

        public static readonly BindableProperty TitleProperty = BindableProperty.Create(
            propertyName: nameof(Title),
            returnType: typeof(string),
            declaringType: typeof(SwitchLabel),
            defaultValue: string.Empty,
            defaultBindingMode: BindingMode.TwoWay);

        public static readonly BindableProperty IsOnProperty = BindableProperty.Create(
            propertyName: nameof(IsOn),
            returnType: typeof(bool),
            declaringType: typeof(SwitchLabel),
            defaultValue: false,
            defaultBindingMode: BindingMode.TwoWay);

        public string Title
        {
            get
            {
                return (string)GetValue(TitleProperty);
            }
            set
            {
                SetValue(TitleProperty, value);
            }
        }

        public bool IsOn
        {
            get
            {
                return (bool)GetValue(IsOnProperty);
            }
            set
            {
                SetValue(IsOnProperty, value);
            }
        }
    }

Do I write it in correct way?
I found that the binding is not firing when I bind a model into it

Please help me


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>