Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Custom Properties in Custom Control not Displayed in Previewer

$
0
0

I have a custom control with 2 custom properties that I want to see in the Xamarin.Forms Previewer in Visual Studio 2019. Here is the code for the custom control:

using System;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ScorePad
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    [DesignTimeVisible(true)]
    public partial class CheckBox : StackLayout
    {
        public event EventHandler<CheckedChangedEventArgs> CheckedChanged = (sender, e) => { };
        public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(String), typeof(CheckBox), string.Empty);
        public static readonly BindableProperty IsCheckedProperty = BindableProperty.Create("IsChecked", typeof(bool), typeof(CheckBox), false);

        public string Text
        {
            get => (string)GetValue(CheckBox.TextProperty);
            set => SetValue(CheckBox.TextProperty, value);
        }
        public bool IsChecked
        {
            get => (bool)GetValue(CheckBox.IsCheckedProperty);
            set => SetValue(CheckBox.IsCheckedProperty, value);
        }

        public CheckBox()
        {
            InitializeComponent();
            this.chkCheckBox.CheckedChanged += this.ChkCheckBox_CheckedChanged;
        }

        private void ChkCheckBox_CheckedChanged(object sender, CheckedChangedEventArgs e) { this.CheckedChanged(this, e); }
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<StackLayout x:Class="ScorePad.CheckBox" x:Name="CheckBoxRoot"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" BackgroundColor="Black" Style="{StaticResource CheckBoxStackLayoutStyle}">
    <CheckBox x:Name="chkCheckBox" Style="{StaticResource CheckBoxBoxStyle}" BindingContext="{x:Reference CheckBoxRoot}" IsChecked="{Binding IsChecked}"/>
    <Label Style="{StaticResource CheckBoxLabelStyle}" BindingContext="{x:Reference CheckBoxRoot}" Text="{Binding Text}"/>
</StackLayout>

Notice that the 2 custom properties (Text & IsChecked) are databound to the controls in the custom control. However, they do not appear in the Previewer, although they do appear when I debug the app. This is my first custom control, so maybe I'm just forgetting something simple, so any help would be appreciated. Thanks.


Viewing all articles
Browse latest Browse all 91519

Trending Articles



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