【发布时间】:2019-04-02 06:10:13
【问题描述】:
我正在尝试通过单击按钮访问在 DataTemplate 中声明的条目,该条目实际上位于 ListView 的 ItemTemplate 中。
<StackLayout>
<Button Text="GetEntryTemplate" Clicked="Button_Clicked"/>
<ListView x:Name="listView" ItemsSource="{Binding Customer}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Entry Text="Xamarin"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
private void Button_Clicked(object sender, EventArgs e)
{
var loadedTemplate = listView.ItemTemplate.CreateContent();
var view = ((loadedTemplate as ViewCell).View as Entry).Text;
}
我试过 CreateContent(),它实际上并没有显示运行时的变化。
谁能帮我解决这个问题。简而言之,我需要通过按钮单击访问现有条目实例(在 DataTemplate 中声明)文本。
【问题讨论】:
-
我认为通过谷歌搜索你可以找到解决方案。根据我对 Windows 窗体的经验,这样的东西应该可以工作 - forums.xamarin.com/discussion/129774/… 但似乎在 Xamarin 的上下文中还有其他方法可能更好。
-
对我来说这听起来像是XY-Problem。你需要这个做什么?当然,运行时更改不会反映,因为您正在创建模板的 new 实例,而不是访问现有实例。
-
列表视图具有视图单元格的集合。那么您到底想引用哪个条目:第一个、选择的、用户录制的?
-
嗨@Arvis,我的收藏中只有一件物品。所以我有一个条目。我需要从后面的代码中访问这个条目文本。
标签: c# xaml xamarin.forms