Dan
Dan

Reputation: 1110

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.

Upvotes: 6

Views: 1211

Answers (2)

Diego Rafael Souza
Diego Rafael Souza

Reputation: 5314

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 MessageObjectobject 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);
} 

Upvotes: 3

sme
sme

Reputation: 4163

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
}

Upvotes: 4

Related Questions