简体   繁体   中英

How to get text from entry

I create an entry using

 <Entry Placeholder="Reply..."/>

It is inside a ListView > ItemTemplate > DataTemplate > ViewCell

The thing is I need a way once a user clicks the submit button in that ViewCell it gets the text for the entry in that cell. I am using Binding to set the values so I don't how to get the text.

When you handle the button's click event, assuming you are using an event handler to listen to the Clicked event, you can get the BindingContext of the button (which should also be the same BindingContext for the entire ViewCell).

Like so:

public void OnButtonClicked(object sender, EventArgs e)
{
    // Assuming the List bound to the ListView contains "MyObject" objects,
    // for example List<MyObject>:

    var myObjectBoundToViewCell = (MyObject)((Button)sender).BindingContext;

    // and now use myObjectBoundToViewCell to get the text that is bound from the Entry
}

Seeing your code I could notice why the @sme's answer doesn't fit you. You're making a very confusing and poor use of bindings and xaml , and I'm quite sure that move to MVVM is the best thing you can do now.

But, if you insist to keep the code like it is now, you can just add the Reply text bound to the Entry's text property, like that:

<Entry Placeholder="Reply..." 
       HorizontalOptions="FillAndExpand" 
       Margin="0, 0, 0, 5"
       Text="{Binding Reply}"/>

So, as you are sending the entire MessageObject object to the tap command, you'll be able to get the text content just this way:

public void ReplyCommandClick(string id, object msg)
{
    MessageObject message = (MessageObject) msg;
    message.ShowReplyField = message.ShowReplyField ? false : true;
    //var viewcell = (MessageObject)((Label)msg).BindingContext;
    //viewcell. // There were no options for the entry
    var reply = msg.Reply;
    SendReply(id, msg, reply);
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM