Home > Enterprise >  How to add a calculated value to a CollectionView after it has been populated
How to add a calculated value to a CollectionView after it has been populated

Time:11-29

I have the following CodeBehind and XAML which I used to get all data from an SQLite table and populate a 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>

How would I update the value in Column 3 (Which currently has P/L as a placeholder for all rows) based on a value from the CollectionView after populating it, and a value from a Label outside the CollectionView without using the MVVM framework?

For example:

(Column 3 label text) = (Column 2 Label text value) - lblTicker.text

CodePudding user response:

We could not change the Column3 label text in the Codebehind way as it is set in the template of the CollectionView. We also cannot access the label in codebehind by setting x:Name to it. Try using data bindings. There are several similar cases that you could refer to: How can I access a specific collectionview child? In this case, the label "DataCadastroLabel" and How to set a x:Name to a control in a CollectionView / ListView in Xamarin.Forms.

CodePudding user response:

How would I update the value in Column 3

Make a custom class, that you use as the ItemTemplate.

Usage:

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

The custom class'es XAML will be similar to what you have now inside DataTemplate, but with a header, like any other XAML file:

<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>

In that class, you can do anything you need when BindingContext is set:

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....;
        ...
    }
}

Update a value from a Label outside the CollectionView

In your page's code behind, its easy to set property values (that are not bound to items of a collection):

lblTicker.Text = "Whatever text is needed".

If you need something more than that, add "pseudo-code" (doesn't actually compile) to the question, that shows an example of what you are trying to do.

  • Related