Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to retrieve value from custom control

nike10nike10 Member ✭✭

This is my custom control with entry, where function checks for valid name and return true or false.. I am not able to set IsValid parameter as well as I am not able to pass IsValid value back to main control..
PatientDetailsEntryFormView .xaml.cs

public partial class PatientDetailsEntryFormView : ContentView
    {
    public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
        public static readonly BindableProperty NameProperty =
            BindableProperty.Create("Name", typeof(string), typeof(PatientDetailsEntryFormView), default(string),BindingMode.TwoWay, propertyChanged:NamePropertyChanged);

        private static void NamePropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control = (PatientDetailsEntryFormView)bindable;
            control._nameEntry.Text = newValue.ToString();
            if (!CheckNameProperty(control._nameEntry.Text))
            {
                control._nameEntry.ErrorText = "A Name is required";                
            }
        }

        static bool CheckNameProperty(string Value)
        {
            if (Value == null)
            {
                return false;
            }

            var str = Value as string;
            return !string.IsNullOrWhiteSpace(str);
        }

        public bool IsValid
        {
            get { return (bool)GetValue(IsValidProperty); }
            set { SetValue(IsValidProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsValid.  This enables animation, styling, binding, etc...
        public static readonly BindableProperty IsValidProperty =
            BindableProperty.Create("IsValid", typeof(bool), typeof(PatientDetailsEntryFormView), default(bool), BindingMode.TwoWay);



        public PatientDetailsEntryFormView()
        {
            InitializeComponent();        
        }

PatientDetailsEntryFormView.xaml

<ContentView x:Name="_patientEntryForm">
                        <Entry
                            x:Name="_nameEntry"
                            Text="{Binding Name, Source={x:Reference _patientEntryForm}}" />
                </ContentView>

MainViewModel.xaml

<templates3:PatientDetailsEntryFormView
                                Name="{Binding PatientName.Value}"
                                IsValid="{Binding IsValid}" />

I had went through dozens of articles before posting question.. Any pointer will be helpful

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    We could change the value of the IsValid in the propertychanged method:

    private static void NamePropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (PatientDetailsEntryFormView)bindable;
        control._nameEntry.Text = newValue.ToString();
        if (!CheckNameProperty(control._nameEntry.Text))
        {
            control._nameEntry.ErrorText = "A Name is required";
            control.IsValid = false;
        }
        else
        {
            control.IsValid = true;
        }
    }
    

    Additionally, you've bound the entry's property to your view model. We could also check its validation in your view model through the value of PatientName.Value.

Sign In or Register to comment.