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

Custom ViewCell with properties exposed that are used by internal controls

$
0
0

I have a custom view cell created with some labels that can have their Text bound in Xaml as to be reuseable. I am doing this to have a layout that is consistent and the other developers I am working with that don't have a lot of experience with Xamarin and Xaml can use easily.

So to be used in the XAML want to be able to do something like:

<local:CustomViewCell NameLabelText="Test" ValueLabelText="{Binding SomeValue}">

The class looks like this

public class CustomViewCell
    {   
    public static readonly BindableProperty NameLabelProperty =
          BindableProperty.Create<CustomViewCell, string>(nl => nl.NameLabelText, "");

    public static readonly BindableProperty ValueLabelProperty =
          BindableProperty.Create<CustomViewCell, string>(nl => nl.ValueLabelText, "");

    public string NameLabelText
        {
            get { return (string)GetValue(NameLabelProperty); }
            set { SetValue(NameLabelProperty, value); }
        }
        public string ValueLabelText
        {
            get { return (string)GetValue(ValueLabelProperty); }
            set { SetValue(ValueLabelProperty, value); }
        }

    public Label NameLabel { get; set; }
        public Label ValueLabel { get; set; }

        public CustomViewCell()
        {
            NameLabel = new Label()
            {
                HorizontalOptions = LayoutOptions.Start,
                HorizontalTextAlignment = TextAlignment.Start,
                VerticalOptions = LayoutOptions.Center
            };
            NameLabel.BindingContext = this;
            NameLabel.SetBinding(Label.TextProperty, (CustomViewCell pc) => pc.NameLabelText, BindingMode.TwoWay);

    ValueLabel = new Label()
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                HorizontalTextAlignment = TextAlignment.End,
                VerticalOptions = LayoutOptions.Center
            };
            ValueLabel.BindingContext = this;
            ValueLabel.SetBinding(Label.TextProperty, (CustomViewCell pc) => pc.ValueLabelText, BindingMode.TwoWay);

    //More layout stuff

The NameLabel sets with the value from XAML but the binding to the ValueLabel doesn't. Is there something I am missing to hook this up or is it not possible?


Viewing all articles
Browse latest Browse all 75885

Trending Articles



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