Text property automatically reset to blank value for custom user control.

maulik25itmaulik25it INMember ✭✭
edited December 2016 in Xamarin.Forms

I have developed simple user control
Here is code for user control

TestControl.Xaml

        <?xml version="1.0" encoding="UTF-8" ?>
        <ContentView x:Class="FixitRate.Controls.TestControl"
                     xmlns="http://xamarin.com/schemas/2014/forms"
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     x:Name="txt">
            <ContentView.Content>
                <Entry Text="{Binding Text, Source={x:Reference txt}, Mode=TwoWay}" />
            </ContentView.Content>
        </ContentView>

TestControl.Xaml.cs

public partial class TestControl : ContentView
{
        public TestControl()
        {
            InitializeComponent();
        }

        public static BindableProperty TextProperty = BindableProperty.Create (
                                                            propertyName: "Text",
                                                            returnType: typeof(string),
                                                            declaringType: typeof(TestControl),
                                                            defaultValue: "",
                                                            defaultBindingMode: BindingMode.TwoWay,
                                                            propertyChanged: HandleButtonPropertyChanged);

        private static void HandleButtonPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            //var button = (ExtendedButton)bindable;
            //button.UpdateButtonState();
        }

        public string Text
        {
            get { return (string)base.GetValue(TextProperty); }
            set { base.SetValue(TextProperty, value); }
        }        
  }

And here is code for my page
<ctr:TestControl Text="{Binding Model.UserName}" />

Here is my model code

public class LoginModel : MVVMBase
{
        #region Propeties
        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set
            {
                _userName = value;
                OnPropertyChanged();
                ValidateProperty(value);
            }
        }

        private string _password = string.Empty;
        public string Password
        {
            get { return _password; }
            set
            {
                SetProperty<string>(ref _password, value);
                ValidateProperty(value);
            }
        }
        #endregion

        public LoginModel()
        {

        }            
  }

When I run a program and try to type some text, It will automatically a rest value to blank.

I put a break point in "HandleButtonPropertyChanged" and checked, First time method raised for set correct value and then second time method raised for reset blank value. I dont know why this happening.

Best Answer

Answers

Sign In or Register to comment.