question

AbhijithHarapanahalli-8372 avatar image
0 Votes"
AbhijithHarapanahalli-8372 asked DaisyTian-1203 answered

WPF Textbox not getting updated from ViewModel after the first time

Hi,
I have a textbox that is bound to a property in the ViewModel. When I run it for the first time and update the property, I see it updated in the UI. But after that, it doesn't get updated at all. I've seen similar posts and I'm doing all the things they've mentioned. I'm stumped. Here's my code:

 <TextBox x:Name="SampleText" Height="23" TextWrapping="Wrap" Margin="0,0,0,3" MinWidth="450" Grid.Column="2" Grid.Row="2" Grid.ColumnSpan="2" Text="{Binding Sample,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>

  <Button x:Name="ClickHere" Content="UpdateText" Width="40" Height="Auto" Command="{Binding ButtonClickCommand}" CommandParameter="{Binding ElementName=UserControlView">
  private string sample;
    
         public string Sample
         {
             get { return sample; }
             set
             {
                 if (value != sample)
                 {
                     sample= value;
                    OnPropertyChanged("Sample");
                 }
             }
         }
 public ButtonClickCommand
 {
    Sample = "Text Updated";
 }


When I run this code for the first time and click the button, the TextBox gets updated in the UI. But when I close the window and open it again, while the code is running, and click the button, the TextBox doesn't get updated.
Help please!


dotnet-csharpdotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered

I make a demo to you based on my understand of your thread, please test it and let me know if it work for you or not.

    public class ViewModel : INotifyPropertyChanged
     {
    
         public event PropertyChangedEventHandler PropertyChanged;
    
         protected void OnPropertyChanged(string propertyName)
         {
             if (PropertyChanged != null)
             {
                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
             }
         }
    
         private string sample;
    
         public string Sample
         {
             get { return sample; }
             set
             {
                 if (value != sample)
                 {
                     sample = value;
                     OnPropertyChanged("Sample");
                 }
             }
         }
    
    
     private MyCommand buttonClickCommand;
    
         public MyCommand ButtonClickCommand
         {
             get
             {
                 if (buttonClickCommand == null)
                     buttonClickCommand = new MyCommand(new Action<object>(o => { Sample = "Text Updated"; }));
                 return buttonClickCommand;
             }
         }
     }
    
     public class MyCommand : ICommand
     {
         private Func<object, bool> _canExecute;
         private Action<object> _execute;
    
         public event EventHandler CanExecuteChanged
         {
             add
             {
                 if (_canExecute != null)
                 {
                     CommandManager.RequerySuggested += value;
                 }
             }
             remove
             {
                 if (_canExecute != null)
                 {
                     CommandManager.RequerySuggested -= value;
                 }
             }
         }
    
    
         public bool CanExecute(object parameter)
         {
             if (_canExecute == null) return true;
             return _canExecute(parameter);
         }
    
         public void Execute(object parameter)
         {
             if (_execute != null && CanExecute(parameter))
             {
                 _execute(parameter);
             }
         }
    
         public MyCommand(Action<object> execute) : this(execute, null)
         {
         }
         public MyCommand(Action<object> execute, Func<object, bool> canExecute)
         {
             _execute = execute;
             _canExecute = canExecute;
         }
     }


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.