Dostonbek Islambekov
Dostonbek Islambekov

Reputation: 11

.NET MAUI Relative Binding Error "Property ... not found on "System.String"

I'm doing the .NET MAUI for beginners tutorial series that dotnet uploaded and my relative binding is just not working, did everything as they said but the error keeps popping.

I'm trying to bind a SwipeItem "Delete" to a DeleteCommand [RelayCommand] (I'm using the CommunityToolkit.Mvvm), the Delete function is located in my MainViewModel.cs

I keep getting the next error: "Binding: Property "DeleteCommand" not found on "System.String". I tried to find anyone else that posted anything about this error but found nothing.

this is my MainPage.xaml file where the error is happening in:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp2.MainPage"
             xmlns:viewmodel="clr-namespace:MauiApp2.ViewModel"
             x:DataType="viewmodel:MainViewModel"
             x:Name="mainPageRef">

    <Grid RowDefinitions="100, Auto, *"
          ColumnDefinitions=".75*, .25*"
          Padding="10"
          RowSpacing="10"
          ColumnSpacing="10">

        <Image Grid.ColumnSpan="2"
               Source="mylogo.png"
               BackgroundColor="Transparent"/>
        <Entry Placeholder="Enter task"
               Text="{Binding Text}"
               Grid.Row="1"/>

        <Button Text="Add"
                Command="{Binding AddCommand}"
                Grid.Row="1"
                Grid.Column="1"/>
        <CollectionView Grid.Row="2" Grid.ColumnSpan="2"
                        ItemsSource="{Binding Items}">
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="{x:Type x:String}">
                    <SwipeView>
                        <SwipeView.RightItems>
                            <SwipeItems>
                                <SwipeItem Text="Delete"
                                           BackgroundColor="Red"
                                           Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MainViewModel}}, Path=DeleteCommand}"
                                           CommandParameter="{Binding .}"/>
                            </SwipeItems>
                        </SwipeView.RightItems>
                        <Grid Padding="0, 5">
                            <Border>
                                <Label Text="{Binding .}"
                                       FontSize="24"/>
                            </Border>
                        </Grid>
                    </SwipeView>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </Grid>
</ContentPage>

I also tried to add AncestorLevel Property but it also didn't help

this is my MainViewModel.cs:

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;


namespace MauiApp2.ViewModel;

public partial class MainViewModel : ObservableObject
{
    public MainViewModel()
    {
        Items = new ObservableCollection<string>();
        System.Diagnostics.Debug.WriteLine("MainViewModel constructor called");

    }

    [ObservableProperty]
    ObservableCollection<string> items;

    [ObservableProperty]
    string text;

    [RelayCommand]
    void Add()
    {
        if (string.IsNullOrWhiteSpace(Text))
            return;

        Items.Add(Text);
        //add our item
        Text = string.Empty;

    }

    [RelayCommand]
    void Delete(string s)
    {
        Console.WriteLine("IN THE DELETE FUNCTION");
        if(Items.Contains(s))
            Items.Remove(s);
    }
}

Thank you for your time!

Upvotes: 1

Views: 125

Answers (1)

Dostonbek Islambekov
Dostonbek Islambekov

Reputation: 11

I just created another project, copied the code from the original project to the new one, and everything works well.

Upvotes: 0

Related Questions