6
votes

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.

2
Please, share the relevant viewcell code. The entry's text default mode is twoway, so when you change the entrys text your bound object is changed tooDiego Rafael Souza
@DiegoRafaelSouza That's all my relevant codeDan
You access the data through each element of the ItemsSource of your ListView. That's the cleanest approach. You only need to get the appropriate indexmr5

2 Answers

3
votes

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);
} 
4
votes

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
}