Overwrite automatically calculated result

Sarah 186 Reputation points
2022-05-24T08:39:17.797+00:00

I have 2 TextBoxes. The first one as input and the second one as output. In the output TextBox a result is displayed depending on the input TextBox. Is there a possibility to overwrite the calculated value (take the input value from the TextBox)?

XAML:

<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>
<Grid>
    <StackPanel VerticalAlignment="Center">
        <TextBox Width="200" Text="{Binding Int1, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox Width="200" Text="{Binding Out, UpdateSourceTrigger=LostFocus}"/>
    </StackPanel>
</Grid>

CS:

public class ViewModel : INotifyPropertyChanged
{
    private int _out;
    private int int1;

    public int Int1
    {
        get { return int1; }
        set
        {
            int1 = value;
            OnPropertyChanged();
            OnPropertyChanged("Out");
        }
    }
    public int Int2 { get; set; } = 50;
    public int Out
    {
        get
        {
            int res = 0;
            if (string.IsNullOrEmpty(Int1.ToString()) == false && Int1 > 0)
            {

                res = Int1 + Int2;
            }
            return res;
        }
        set
        {
            _out = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    internal void OnPropertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
Developer technologies | Windows Presentation Foundation
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2022-05-24T09:26:18.297+00:00

    ViewModel:

    public class ViewModel : INotifyPropertyChanged  
      {  
        private int _out;  
        private int int1;  
        public int Int1  
        {  
          get { return int1; }  
          set  
          {  
            int1 = value;  
            OnPropertyChanged("Int1");  
            OnPropertyChanged("Out");  
          }  
        }  
        private int int2= 50;  
        public int Int2  
        {  
          get { return int2; }  
          set { int2 = value; OnPropertyChanged("Out"); OnPropertyChanged("Int2"); }  
        }  
        public int Out  
        {  
          get { return Int1 + Int2; }  
        }  
        public event PropertyChangedEventHandler PropertyChanged;  
         
        internal void OnPropertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));  
      }  
    

    The result:
    205295-77.gif


    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.


  2. Peter Fleischer (former MVP) 19,341 Reputation points
    2022-05-29T03:55:01.503+00:00

    Hi Sarah,
    I think your ViewModel must be like this:

      public class ViewModel : INotifyPropertyChanged
      {
        private int _out;
        private int _int1;
        private int _int2 = 50;
    
        public int Int1
        {
          get => _int1;
          set
          { _int1 = value; OnPropertyChanged(); SetValueOut(); }
        }
        public int Int2
        {
          get => _int2;
          set { _int2 = value; OnPropertyChanged(); SetValueOut(); }
        }
    
        public int Out
        {
          get => _out;
          set { _out = value; OnPropertyChanged(); }
        }
    
        private void SetValueOut()
        {
          _out = (_int1 == 0) ? 0 : _int1 + Int2;
          OnPropertyChanged(nameof(Out));
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        internal void OnPropertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
      }
    
    0 comments No comments

Your answer

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