Reputation: 456
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?
Upvotes: 1
Views: 3027
Reputation: 89102
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
Upvotes: 3
Reputation:
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:
<ListView>
and related from the XAML fileListView
object and add it to the StackLayout
object. Save the ListView
object to a field like ListView _myListView
StackLayout.Remove(_myListView)
Upvotes: 0