Search code examples
xamarinxamarin.formsxamarin-studio

How to create custom textchanged event on my custom item


i want create textchanged event with binding but idk how to do. im making custom numeric up down and i want textchanged event but im using frame on main view help.

public class NumericUpDown : Frame
{
    Grid _grid;
    CustomNumericUpDownEntry _entry;
    StackLayout _stackLayout;
    Button _btnup;
    Button _btndown;
    double _value = 0, _maxValue = 60, _minValue = 0, _increaseValue = 1;
    int _btnCornerRadius = 15;
    Color _btnBackColor = Color.White;
    public NumericUpDown() : base()
    {
        BindingContext = this;
        _grid = new Grid();
        _entry = new CustomNumericUpDownEntry();
        _stackLayout = new StackLayout();
        _btnup = new Button();
        _btndown = new Button();

       ...

        _stackLayout.Children.Add(_btndown);
        _stackLayout.Children.Add(_btnup);
        _grid.Children.Add(_entry);
        _grid.Children.Add(_stackLayout);

        Content = _grid;
    }
    ...
}

its looking like this

<customitems:NumericUpDown
        Margin="10,0"
        BackgroundColor="Transparent"
        BorderColor="red"
        ButtonBackgroundColor="Purple"
        ButtonCornerRadius="15"
        CornerRadius="15"
        HorizontalOptions="Center" />

Solution

  • I found this and worked.

        public NumericUpDown() : base()
        {
            BindingContext = this;
            _entry = new CustomNumericUpDownEntry();
            ...
            _entry.TextChanged += (sender, args) =>
            {
                OnTextChanged(args);
            };
            ...
        }
    

    Public event

        public event EventHandler<TextChangedEventArgs> TextChanged;
        protected virtual void OnTextChanged(TextChangedEventArgs e)
        {
            EventHandler<TextChangedEventArgs> handler = TextChanged;
            handler?.Invoke(this, e);
        }
    

    xaml

    <customitems:NumericUpDown
     x:Name="numRiskLimit"
     ...
     TextChanged="numRiskLimit_TextChanged"
     ... />