Doesnt like NotifyPropertyChanged(() => IsChecked);

Ronald Rex 1,666 Reputation points
2023-08-11T13:59:19.54+00:00

The lines where I have NotifyPropertyChanged(()=IsChecked) I get a compiler error that says the name NotifyPropertyChanged does not exist in the current context. Also, I was wondering are those methods NotifyPorpertyChanged Delegates that point to the anonymous lambda expression () => IsChecked? Also below I am having issues with my VIEW giving me compiler errors. line local:KeypadViewModel the compiler error says The type 'local:KeypadViewModel was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. And on the EntryText line it says The property content is set more than once.



public class MyViewModel : INotifyPropertyChanged

{

const string Msg1 = "blah 1";

const string Msg2 = "blah 2";

private bool _isChecked;

public bool IsChecked

{

    get { return _isChecked; }

    set

    {

        if (_isChecked == value) return;

        _isChecked = value;

        MyBoundMessage = _isChecked ? Msg1 : Msg2;

        //NotifyPropertyChanged(() => IsChecked);

        //NotifyPropertyChanged(() => MyBoundMessage);

    }

}

public string MyBoundMessage { get; set; }

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)

{

    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

}

}

<?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"              xmlns:local="clr-namespace:MauiApp3"              
x:Class="MauiApp3.MainPage"              
Title="Keypad Page">     
<ContentPage.BindingContext>         
<local:KeypadViewModel/>     
</ContentPage.BindingContext>     
<CheckBox IsChecked="{Binding IsChecked}" />     
<Entry Text="{Binding MyBoundMessage}" />  
</ContentPage>
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,071 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,411 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 121.3K Reputation points
    2023-08-11T14:17:10.7233333+00:00

    Instead of NotifyPropertyChanged, try this: OnPropertyChanged("IsChecked"); OnPropertyChanged("MyBoundMessage"). The latter can be moved to the setter of MyBoundMessage.

    You can also write OnPropertyChanged(nameof(IsChecked)).

    The lambda expressions are not used in this approach.

    Also try <local:MyViewModel/>.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.