我使用以下代码创建一个条目:
<Entry Placeholder="Reply..."/>
这是在ListView > ItemTemplate > DataTemplate > ViewCell内部。
问题是,当用户单击该ViewCell中的提交按钮时,需要获取该单元格中输入框的文本。我正在使用绑定来设置值,因此不知道如何获取文本。
我使用以下代码创建一个条目:
<Entry Placeholder="Reply..."/>
这是在ListView > ItemTemplate > DataTemplate > ViewCell内部。
问题是,当用户单击该ViewCell中的提交按钮时,需要获取该单元格中输入框的文本。我正在使用绑定来设置值,因此不知道如何获取文本。
当您处理按钮的点击事件时,假设您正在使用事件处理程序来监听Clicked
事件,那么您可以获取按钮的BindingContext
(这也应该是整个ViewCell的相同BindingContext
)。
如下所示:
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
}
bindings
和xaml
的使用非常混乱和糟糕,我相信现在转向MVVM
是你能做的最好的事情。<Entry Placeholder="Reply..."
HorizontalOptions="FillAndExpand"
Margin="0, 0, 0, 5"
Text="{Binding Reply}"/>
MessageObject
对象发送到tap命令,您可以通过以下方式获取文本内容: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);
}
ListView
的ItemsSource
中的每个element
来获取数据。这是最简洁的方法,您只需要获取相应的索引即可。 - mr5