我有下面的CodeBehind
和XAML
,我使用它们从SQLite
表中获取所有数据并填充CollectionView
:
.cs文件系统
protected override void OnAppearing()
{
base.OnAppearing();
List<Record> records = App.RecordRepo.GetAllRecords();
recordList.ItemsSource = records;
}
.xaml格式
<Grid Grid.Row="0">
<VerticalStackLayout>
<Label x:Name="lblHoldingTotal" Text="Total"/>
<Label x:Name="lblAverageBuyPrice" Text="Average Buy Price"/>
<Label x:Name="lblTotalPaid" Text="Total Paid"/>
<Label x:Name="lblTicker" Text="Ticker"/>
<Label x:Name="lblHoldingValue" Text="Holding Value"/>
<Label x:Name="lblProfit" Text="Profit"/>
</VerticalStackLayout>
</Grid>
<Grid Grid.Row="1">
<CollectionView x:Name="recordList">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding Id}" />
<Label Grid.Column="1" Text="{Binding Amount}" />
<Label Grid.Column="2" Text="{Binding Paid}" />
<Label Grid.Column="3" Text="P/L" />
<Label Grid.Column="4" Text="{Binding PurchaseDate}" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>
如何在不使用MVVM框架的情况下,基于填充后的CollectionView
中的值和CollectionView
之外的Label
中的值更新第3列(当前将P/L
作为所有行的占位符)中的值?
例如:(Column 3 label text) = (Column 2 Label text value) - lblTicker.text
2条答案
按热度按时间bqujaahr1#
我们无法以Codebehind方式更改Column3标签文本,因为它是在CollectionView的模板中设置的。我们也无法通过将x:Name设置为Codebehind方式访问该标签。请尝试使用数据绑定。有几个类似的情况可供参考:How can I access a specific collectionview child? In this case, the label "DataCadastroLabel"和How to set a x:Name to a control in a CollectionView / ListView in Xamarin.Forms的两个值。
h9vpoimq2#
如何更新第3列中的值
创建一个自定义类,用作ItemTemplate。
用法:
自定义类的XAML将类似于您现在在
DataTemplate
中所拥有的内容,但与任何其他XAML文件一样,它具有一个标题:在该类中,当设置了BindingContext时,可以执行任何所需的操作:
从CollectionView外部的标签更新值
在网页的程式码后置(Behind)中,设定属性值(未系结至集合项目)很容易:
如果你需要更多的东西,在问题中添加“伪代码”(实际上没有编译),这是一个你试图做什么的例子。