Getting last 4 characters from a string retrieved from a Database

I'm using Firebase Realtime Database and I'm getting a string from the database and displaying it in a view using XAML. In order to cut a string and get last 4 digits, I'd generally do

x:Name="string1"

x.xaml.xs

string1.Text.Substring(string1.Text.Length - 4)

but I'm getting string1 doesn't exist in the current context. Maybe it's because I have it inside a ListView and I'm binding string1?

`
<ListView.ItemTemplate>


<ViewCell.View>

                                    <Label x:Name="string1" VerticalTextAlignment="Center" Text="{Binding itemID}"/>
                                    <Label VerticalTextAlignment="Center" Text="{Binding item}" />

                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>`

Best Answer

Answers

  • leo-mckleo-mck BRMember ✭✭

    Yes, you can't get that element. Think about it, if you are inside a ListView with multiple items, you would end up having multiple "string1" items. There is ways to do this but I think the best one for you is to use a ValueConverter. A ValueConverter purpose is to get a value and return it on a specific format for the view. In this case you would receive a full string and return the last 4 characters. Look here for an example on how to use a ValueConverter: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters

Sign In or Register to comment.