Search code examples
xamarinmvvmxamarin.formspopup

Button Command not finding Binded ViewModel using rg.plugin.popup


the issue appears when trying to bind the properties of the view to any source created on ViewModel, an example is the label's Text property<-binding->test(below on code) not works, but Text property of Button i make a bind to another object sended to this viewModel and work's normally. I just would like to understand why the Binding is not working as usual. Here's the code.

XAML.cs

public PopupView(Func<bool> metodo, Tarefa tarefa)
{
    BindingContext = new ViewModels.Popups.PopupViewModel(metodo, tarefa);
    InitializeComponent();
}

XAML

<pages:PopupPage 
         xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
         xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="Taskinho.Views.Popups.PopupView"
         xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup">
    <StackLayout>
        <Label HorizontalOptions="Center" Text="{Binding test}" />
        <Button Text="{Binding tarefa.TarefaTitulo}" Command="{Binding Confirmar}" />
    </StackLayout>
</pages:PopupPage>

ViewModel

//Property
private Tarefa _Tarefa;
public Tarefa tarefa
{
    get { return _Tarefa; }
    set { _Tarefa = value;
        NotifyPropertyChanged("Tarefa");
    }
}
//another property
public string test = "Test Name";


//Constructor
public PopupViewModel(Func<bool> metodoParam, Tarefa TarefaParam)
{            
    this.tarefa = new Tarefa();
    ExecutarCommand = new Command(ExecutarAction);
}

//The binded Command(Not finded by the binding)
public Command ExecutarCommand
{
    get;
    set;
}
//Action of Command
void ExecutarAction()
{
    //do something
}

I tried to use Path="" and Source="" on Button binding but i don't know how to use for this situation.

Project Link Open/Public on Git

Thank's


Solution

  • the label's Text property<-binding->test(below on code) not works, but Text property of Button i make a bind to another object sended to this viewModel and work's normally. I just would like to understand why the Binding is not working as usual

    Firstly, you can only bind property, not field. replace your code:

     private string _test;
        public string test
        {
            get { return _test; }
            set
            {
                _test = value;
                NotifyPropertyChanged("test");
            }
        }   
        public PopupViewModel(Func<bool> metodoParam, Tarefa TarefaParam)
        {
            this.tarefa = new Tarefa();
            ExecutarCommand = new Command(ExecutarAction);
            test = "this is test!";
        }
    

    About Path="" and Source="", I do one sample for you.

    Here is viewmodel class:

    public class viewmodel1: INotifyPropertyChanged
    {
        private string _test;
        public string test
        {
            get
            {
                return _test;
            }
            set
            {
                _test = value;
                RaisePropertyChanged("test");
            }
        }
        public  Command command1 { get; set; }
    
        public viewmodel1()
        {
            test = "this is test!";
            command1 = new Command(method1);
        }
    
        private void method1()
        {
            Console.WriteLine("this is test!!!!!!!");
        }
    
      
        public event PropertyChangedEventHandler PropertyChanged;
        
        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
    
    }
    

    Here is the view:

    <ContentPage
    x:Class="demo2.simplecontrol.Page18"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:model="clr-namespace:demo2.simplecontrol">
    <ContentPage.BindingContext>
        <model:viewmodel1 x:Name="data1" />
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout>
            <Label
                HorizontalOptions="CenterAndExpand"
                Text="{Binding Path=test, Source={x:Reference data1}}"
                VerticalOptions="CenterAndExpand" />
    
            <Button Command="{Binding Path=command1, Source={x:Reference data1}}" Text="click1" />
        </StackLayout>
    </ContentPage.Content>
    </ContentPage>
    

    More detailed information, you can take a look:

    https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/binding-path