Home > Enterprise >  .NET MAUI Binding a List to a Context View
.NET MAUI Binding a List to a Context View

Time:08-12

So I have a list which shows Tags which are supposed to be able to be deleted created and modified and for that i want it to be binded with the context view

On the Xaml file i have wrote this

<ScrollView
                WidthRequest="1400"
                HeightRequest="600"
                HorizontalOptions="StartAndExpand"
                >
                <VerticalStackLayout
                    Spacing="5"
                    >
                    <CollectionView
                        x:Name="Tags"
                        ItemsSource="{Binding TagList}"  ** Here would be the binding i think
                        SelectionMode="None"
                        >
                        <CollectionView.ItemTemplate>
                            <DataTemplate>
                                <HorizontalStackLayout
                                    VerticalOptions="StartAndExpand" 
                                    Margin="30,0,0,0">
                                    <controls:TagView
                                        Padding="5"
                                        TagTitle="{Binding TagTitle, FallbackValue='1'}"
                                        TagNr="{Binding TagNr, FallbackValue='1'}"
                                        ValField="{Binding ValField}"
                                        ShowBtn="{Binding ShowBtn}"
                                        />
                                </HorizontalStackLayout>
                            </DataTemplate>
                        </CollectionView.ItemTemplate>
                    </CollectionView>
                </VerticalStackLayout>
            </ScrollView>


and here is the C# code to that


public static List<TagView> TagList { get; set; } = new();
 
public MainPage()
    {
        InitializeComponent();
        Tags.BindingContext = TagList;

    }

i have already tried to bind it per ItemsSource but if i did it with that, then it wont update changes to the list

If you could help then i would appreciate it. Thanks!!

CodePudding user response:

You probably want to use an ObservableCollection<TagView> instead of a List. An ObservableCollection will notify the UI of changes in the collection and update accordingly.

Basically the ObservableCollection has INotifyPropertyChanged functionality built-in. If you want to use a List you will have to notify the UI manually that an update has happened.

  • Related