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

OnTapped event is not firing in my customcell of ListView

$
0
0

Hello All,
I have a List View and data template for that is a custom cell.My custom cell is inheriting ViewCell.
`public class CustomSelectableCell : ViewCell
{
private StackLayout _stackLayout;
private Label _text;
private MR.Gestures.Frame _frame;
private MR.Gestures.Frame _containerFrame;

    /// <summary>
    /// The BindableProperty
    /// </summary>
    public static readonly BindableProperty CommandProperty = BindableProperty.Create<CustomSelectableCell, Command>(i => i.Command, default(Command));
    /// <summary>
    /// Gets/Sets the command which gets executed on tapped
    /// </summary>
    public Command Command
    {
        get
        {
            return (Command)this.GetValue(CommandProperty);
        }
        set
        {
            this.SetValue(CommandProperty, value);
        }
    }
    /// <summary>
    /// Then BindableProperty
    /// </summary>
    public static readonly BindableProperty AutoSelectProperty = BindableProperty.Create<CustomSelectableCell, bool>(i => i.AutoSelect, true, BindingMode.TwoWay);
    /// <summary>
    /// Gets/Sets if the cell will automatically select the itself by tapping
    /// </summary>
    public bool AutoSelect
    {
        get
        {
            return (bool)this.GetValue(AutoSelectProperty);
        }
        set
        {
            this.SetValue(AutoSelectProperty, value);
        }
    }
    //private Grid _gridLayout;
    //private Label _text;
    //private Image _image;

    /// <summary>
    /// The BindableProperty
    /// </summary>
    public static readonly BindableProperty IsSelectedProperty = BindableProperty.Create<CustomSelectableCell, bool>(i => i.IsSelected, default(bool), BindingMode.TwoWay, null, IsSelectedChanged);
    /// <summary>
    /// Gets/Sets if the cell is in selected state or not
    /// </summary>
    public bool IsSelected
    {
        get
        {
            return (bool)this.GetValue(IsSelectedProperty);
        }
        set
        {
            this.SetValue(IsSelectedProperty, value);
        }
    }
    /// <summary>
    /// The BindableProperty
    /// </summary>
    public static readonly BindableProperty TextProperty = BindableProperty.Create<CustomSelectableCell, string>(i => i.Text, default(string), BindingMode.TwoWay, null, TextChanged);
    /// <summary>
    ///  Gets/Sets the text of the cell
    /// </summary>
    public string Text
    {
        get
        {
            return (string)this.GetValue(TextProperty);
        }
        set
        {
            this.SetValue(TextProperty, value);
        }
    }
    /// <summary>
    /// Creates a new instance of <c>CustomSelectableCell</c>
    /// </summary>
    public CustomSelectableCell()
    {


        this._text = new Label
        {
            Style = Device.Styles.ListItemTextStyle
        };

// this._containerFrame = new MR.Gestures.Frame {
// OutlineColor=Color.Green
// };

        this._frame = new MR.Gestures.Frame
        {
            WidthRequest=150,
            HeightRequest=200,
            BackgroundColor=Color.White,
            IsVisible = false,
            HorizontalOptions=LayoutOptions.CenterAndExpand,
            VerticalOptions=LayoutOptions.CenterAndExpand,


        };

        this._stackLayout = new StackLayout()
        {
            HorizontalOptions = LayoutOptions.StartAndExpand,
            Orientation = StackOrientation.Vertical,
            Children = { _text,_frame },

        };
        //_containerFrame.Content = this._stackLayout;

        //this.View = this._containerFrame;
        this.View = this._stackLayout;

    }

    /// <summary>
    /// Raised when <c>IsSelected</c> changed
    /// </summary>
    /// <param name="obj">The cell</param>
    /// <param name="oldValue">Old value</param>
    /// <param name="newValue">New value</param>
    private static void IsSelectedChanged(BindableObject obj, bool oldValue, bool newValue)
    {
        var cell = obj as CustomSelectableCell;

        if (cell != null)
        {

            cell._frame.IsVisible = newValue;
        }
    }
    /// <summary>
    /// Raised when the <c>Text</c> changed
    /// </summary>
    /// <param name="obj">The cell</param>
    /// <param name="oldValue">Old value</param>
    /// <param name="newValue">New value</param>
    private static void TextChanged(BindableObject obj, string oldValue, string newValue)
    {
        var cell = obj as CustomSelectableCell;

        if (cell != null)
        {
            cell._text.Text = newValue;
        }
    }

    /// <summary>
    /// Auto Select on tapped
    /// </summary>
    protected override void OnTapped()
    {
        base.OnTapped();

        if (this.AutoSelect) {
            this.IsSelected = !this.IsSelected;

        }
        if (this.Command != null)
        {
            if (this.Command.CanExecute(this))
            {
                this.Command.Execute(this);
                this._frame.Content = new Label {
                    Text = this.Text,
                    FontSize = 15,
                    TextColor = Color.Black,
                    HorizontalOptions = LayoutOptions.CenterAndExpand,
                    VerticalOptions = LayoutOptions.CenterAndExpand 
                };
            }
        }
    }
}`

this is working fine but if i put the stackLayout in a Frame and putting Frame as a view then OnTapped event is not firing.

Any help or suggestion will be appreciated.


Viewing all articles
Browse latest Browse all 75885

Trending Articles



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