簡體   English   中英

如何將按鈕單擊事件包裝到正確的ViewCell

[英]How to wrap a button click event to the right ViewCell

我創建了一個CustomCell並在上面放了一個按鈕。

http://i1068.photobucket.com/albums/u451/tasknick/Captura%20de%20Tela%202015-06-15%20as%2010.05.10_zpsit980vqh.png

public class CustomCell : ViewCell
{
    public CustomCell ()
    {
        var Name = new Label {
            TextColor = Color.Black,
            FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
            VerticalOptions = LayoutOptions.Start, HorizontalOptions = LayoutOptions.Start,
            FontFamily = Device.OnPlatform ("GillSans", "Quattrocento Sans", "Comic Sans MS")
        };
        Name.SetBinding (Label.TextProperty, "FirstName", BindingMode.TwoWay);

        var LastName = new Label {
            TextColor = Color.Gray,
            FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)), 
            VerticalOptions = LayoutOptions.End, HorizontalOptions = LayoutOptions.Start 
                , FontFamily = Device.OnPlatform ("GillSans", "Quattrocento Sans", "Comic Sans MS")
        };
        LastName.SetBinding (Label.TextProperty, "LastName", BindingMode.TwoWay);

        var ActionButton = new Button {
            Image = Images.ActionButton,
            Style = Styles.DefaultButtonStyle,
            HorizontalOptions = LayoutOptions.End,
            VerticalOptions = LayoutOptions.End
        };
        ActionButton.SetBinding (Button.CommandProperty, "commandActionButton", BindingMode.TwoWay);

        StackLayout stack = new StackLayout {
            Padding = new Thickness (20, 0, 0, 0),
            Orientation = StackOrientation.Horizontal,
            HorizontalOptions = LayoutOptions.StartAndExpand,
            Children = { Name, LastName, ActionButton } 
        }

            {CODE}

        View = layout;

        ActionButton.Clicked += (object sender, System.EventArgs e) => Debug.WriteLine ("asdjhadjsad");
    }
}

按鈕的click事件起作用。 但是我怎么知道這個事件發生在什么地方呢? 例如:當我單擊第一個單元格上的按鈕時,我想顯示第一個單元格中的文本;

  1. 為什么要處理ActionButton.Clicked而不是Command 您已在此處定義綁定

    ActionButton.SetBinding(Button.CommandProperty,“ commandActionButton”,BindingMode.TwoWay);

這樣您就可以在view-model處理click了。

  1. 如果要處理此事件- sender應該是按鈕的實例。

嘗試使用FrameworkElement.Parent屬性並將其強制轉換為自定義單元格。 這將為您提供按住按鈕的單元格上的手柄。 一個粗略的例子是“((CustomCell)sender.Parent)”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM