XAML 如何在填充CollectionView后向其中添加计算值

zbwhf8kr  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(126)

我有下面的CodeBehindXAML,我使用它们从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

bqujaahr

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的两个值。

h9vpoimq

h9vpoimq2#

如何更新第3列中的值

创建一个自定义类,用作ItemTemplate。
用法:

<CollectionView.ItemTemplate>
    <DataTemplate>
        <mynamespace:MyItemView ... />
    </DataTemplate>
</CollectionView.ItemTemplate>

自定义类的XAML将类似于您现在在DataTemplate中所拥有的内容,但与任何其他XAML文件一样,它具有一个标题:

<Grid xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyNameSpace.MyItemView">
    ...
    <... x:Name="someElement" ... />
</Grid>

在该类中,当设置了BindingContext时,可以执行任何所需的操作:

public partial class MyItemView : Grid
{
    ...
      
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();

        // The "model" that this row is bound to.
        var item = (MyItemClass)BindingContext;
        // The UI element you want to set dynamically.
        someElement.SomeProperty = item....;
        ...
    }
}

从CollectionView外部的标签更新值

在网页的程式码后置(Behind)中,设定属性值(未系结至集合项目)很容易:

lblTicker.Text = "Whatever text is needed".

如果你需要更多的东西,在问题中添加“伪代码”(实际上没有编译),这是一个你试图做什么的例子。

相关问题