0

I have text block that i want to chance from false to true by his binding property. The property has change to true but the text of text box stay false. How can I do this right. Thank for the help.

    <TextBlock x:Name="resBlock" Grid.Row="3" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="250" Height="50" Text="{Binding Source={StaticResource Locator}, Path=Main.Result}" TextAlignment="Center" FontSize="30" />
    public class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            LoginCommand = new RelayCommand(Login);
            user = new User();
        }
        DataService service = new DataService();
        public User user { get; set; }
        public bool Result { get; set; }
    
        public ICommand LoginCommand { get; }
    
        public async void Login()
        {
            Result = await service.LoginAsync(user); // get True
        }
    }
Ron Atali
  • 3
  • 3

1 Answers1

0

To change the amount of control with the viewmodel, you must implement the INotifyPropertyChanged interface.

change MainViewModel to:

public class MainViewModel : ViewModelBase, INotifyPropertyChanged
{
   public MainViewModel()
   {
      LoginCommand = new RelayCommand(Login);
      user = new User();
   }
   DataService service = new DataService();
   public User user { get; set; }
    
   public ICommand LoginCommand { get; }
    
   public async void Login()
   {
       Result = await service.LoginAsync(user); // get True
   }
   private bool result;

   public bool Result
   {
      get { return result; }
      set
      {
          result = value;
          OnPropertyChange(nameof(Result));
      }
   }

   public event PropertyChangedEventHandler PropertyChanged;

   protected void OnPropertyChange(string propertyName)
   {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }
}
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17