Search code examples
xamarin.formsbindingcommandrelaycommand

Clear entry text from ViewModel using RelayCommand


I would like to clear entry text from my ViewModel which is binded there. In the code below I tried it by using a RelayCommand, but it doesn't work.

What i want to accomplish: When clicking button named AddQuestionToQuiz, a function is executed by using Command on the button. The function OnCreateQuizClick(), located in my ViewModel, is triggerd and this function needs to clear my entry text, which i don't get for the moment.

I also tried to use a regular Command instead of using a RelayCommand, but also here it doesn't want to work.

EDIT: UNDERNEATH CODE WORKS FINE - GOT UPDATED Code is used to clear entry text when clicking on a button from your ViewModel, implementing INotifyPropertyChanged Interface

.xaml - code

<Button x:Name="AddQuestionToQuiz" WidthRequest="200" Command="{Binding CreateQuizCommand}" Style="{StaticResource ButtonStyle}" Text="Add question to quiz"></Button>

ViewModel - code

internal class CreateQuizPageViewModel : INotifyPropertyChanged
{
    // Quiz Name Input
    public String QuizNameInput { get; set; }

    private String quizQuestionInput = "";
    public String QuizQuestionInput 
    {
        get { return quizQuestionInput; }   
        set { quizQuestionInput = value; OnPropertyChanged(); }
    } 

    public RelayCommand CreateQuizCommand { get; set; }

    public CreateQuizPageViewModel()
    {
        CreateQuizCommand = new RelayCommand(OnCreateQuizClick);
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public void OnCreateQuizClick()
    {
        QuizQuestionInput = "";
    }
}

Solution

  • EDIT: VIEWMODEL UPDATED

    .xaml - code

    <Button x:Name="AddQuestionToQuiz" WidthRequest="200" Command="{Binding CreateQuizCommand}" Style="{StaticResource ButtonStyle}" Text="Add question to quiz"></Button>
    

    ViewModel - code

    internal class CreateQuizPageViewModel : INotifyPropertyChanged
    {
        // Quiz Name Input
        public String QuizNameInput { get; set; }
    
        private String quizQuestionInput = "";
        public String QuizQuestionInput 
        {
            get { return quizQuestionInput; }   
            set { quizQuestionInput = value; OnPropertyChanged(); }
        } 
    
        public RelayCommand CreateQuizCommand { get; set; }
    
        public CreateQuizPageViewModel()
        {
            CreateQuizCommand = new RelayCommand(OnCreateQuizClick);
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void OnPropertyChanged(string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
        public void OnCreateQuizClick()
        {
            QuizQuestionInput = "";
        }
    }