【发布时间】:2017-05-07 17:27:36
【问题描述】:
所以我有以下代码为我的TableView 动态创建 ViewCells:
XAML:
<StackLayout>
<TableView Intent="Settings">
<TableView.Root>
<TableSection x:Name="tableSection"></TableSection>
</TableView.Root>
</TableView>
</StackLayout>
C#:
categories = getCategories();
foreach (var category in categories)
{
var viewCell = new ViewCell
{
View = new StackLayout()
{
Padding = new Thickness(20, 0, 20, 0),
HorizontalOptions = LayoutOptions.FillAndExpand,
Children = {
new StackLayout() {
Orientation = StackOrientation.Horizontal,
VerticalOptions = LayoutOptions.CenterAndExpand,
Children = {
new StackLayout() {
HorizontalOptions = LayoutOptions.StartAndExpand,
Children = {
new Label { Text = category.Name}
},
new StackLayout() {
HorizontalOptions = LayoutOptions.EndAndExpand,
Orientation = StackOrientation.Horizontal,
Children = {
new Label { Text = category.Count},
new Image { Source = "right1.png",
IsVisible = category.Selected }
}
}
}
}
}
}
};
viewCell.Tapped += (sender, e) =>
{
if (category.Selected == false)
{
App.DB.UpdateSelected(true);
}
else
{
App.DB.UpdateSelected(false);
}
categories = getCategories();
totalPhraseCount = getTotalPhraseCount();
Title = totalPhraseCount.ToString() + " phrases";
};
tableSection.Add(viewCell);
}
我想要做的是,每当我点击视图单元格以更新所选属性时,表格视图中的数据也会更新。在 ListView 中,我可以调用 ItemSelected 事件并使用更新的类别再次调用 ItemSource。 TableView 可以吗?
【问题讨论】:
-
为什么要使用TableView?回答这个问题对于实施适当的解决方案很重要。如果您在 TableView 中可能有很多行,您将不得不对您的解决方案进行创意。我想提供帮助,但需要更多关于您尝试使用此 TableView 在应用程序中实现的目标的指导(例如它是设置页面,还是向用户显示动态行等)。
标签: c# .net xaml xamarin xamarin.forms