I am new with MAUI and I cannot find any solution in the web, I want just delete any control or component. A component can be a listView, button, or anything.
For example I the following code:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp2.Prueba"
Title="Prueba">
<StackLayout VerticalOptions="Center">
<ListView x:Name="FruitListView">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding FruitName}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button x:Name="btnDelete"
Text="Random Color"
HorizontalOptions="Center"
VerticalOptions="Center"
Clicked="btnDelete_Clicked" />
</StackLayout>
How can I delete the entire <ListView> when I click the button btnDelete?
I have this:
private void btnDelete_Clicked(object sender, EventArgs e)
{
FruitListView. ;
}
I cant find any reference to delete this ListView component, is it possible through backend code?
CodePudding user response:
You can remove a control from its parent layout like this
MyStackLayout.Children.Remove(FruitListView);
You will need to assign an x:Name to the parent layout in order to reference it in the code behind
CodePudding user response:
I don't believe you can delete a control at runtime if it began its life in the original XAML at compile time. WPF does much in the way of custom code generation during compilation and MAUI is no different.
A better approach would be to move the ListView from adding-to-the-window-at-compilation-time to adding-to-the-window-at-runtime.
In other words:
- delete the
<ListView>and related from the XAML file - in your page's code-behind C# .CS file, create a
ListViewobject and add it to theStackLayoutobject. Save theListViewobject to a field likeListView _myListView - Later you can just
StackLayout.Remove(_myListView)
