Search code examples
c#wpfmvvminotifypropertychanged

Usercontrol doesn`t update textbox mvvm WPF C#


sorry for so much code. But unfortunately there is so much connected. That's why I had to insert so much code. I tried to keep it to a minimum. I hope it is enough.

To my problem: As you can see here there is a button to select a path on the computer. There is also a textbox which shows the path again.

XAML UserControl Code:

<DockPanel  Grid.Row="1" Grid.Column="1">
        <Button 
            Content="Repo Pfad" 
            Command="{Binding SelectRepoPathCommand}"/>
    </DockPanel>

    <DockPanel  Grid.Row="1" Grid.Column="2">
        <TextBox Text="{Binding repoPath, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
    </DockPanel>

Here the code is stored in the config for the user. So that he does not have to enter the path again and again at the next start. Small additional Info: Since the path to the config is saved successfully, the test box will be updated after the second time you select the path. This is because the second time the previously saved path is displayed.

ViewModel:

class BuildToolViewModel : ObservableObject
    {
       private string _repoPath;
        public string repoPath
        {
            get
            {
                if (Properties.Settings.Default.repoPath != null)
                {
                    return Properties.Settings.Default.repoPath;
                }
                else
                {
                    return _repoPath;
                }
            }
            set
            {
                _repoPath = value;
                OnPropertyChanged("repoPath");
                Properties.Settings.Default.repoPath = _repoPath;
                Properties.Settings.Default.Save();
            }
        }

 public RelayCommand SelectRepoPathCommand{ get; private set; }

    #endregion #Properties

    #region ctor
    public BuildToolViewModel()
    {
        SelectRepoPathCommand = new RelayCommand(SelectRepoPath);
    }
    #endregion //ctor


    #region Methods
    public void SelectRepoPath(object sender)
    {
        repoPath = explorerDialog.OpenFileDialogPath();
    }
}

Here is my ObservableObject that inherits from INotifyPropertyChanged.

ObservableObject (INotifyPropertyChanged):

class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            this.VerifyPropertyName(propertyName);

            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
}

Here is my RelayCommand that inherits from ICommand.

RelayCommand (ICommand):

 class RelayCommand : ICommand
    {
        #region Fields

        readonly Action<object> _execute;
        readonly Predicate<object> _canExecute;

        #endregion // Fields

        #region ctor

        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        #endregion //ctor

        #region ICommand Members

        [DebuggerStepThrough]
        public bool CanExecute(object parameters)
        {
            return _canExecute == null ? true : _canExecute(parameters);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameters)
        {
            _execute(parameters);
        }

        #endregion // ICommand Members
    }

Here still like it in the MainWindowViewModel with the ICommand implement.

MainWindowViewModel (ObservableObject):

class MainWindowViewModel : ObservableObject
    {
        private ICommand _changePageCommand;

        private IPageViewModel _currentPageViewModel;
        private List<IPageViewModel> _pageViewModels;


            public ICommand ChangePageCommand
        {
            get
            {
                if (_changePageCommand == null)
                {
                    _changePageCommand = new RelayCommand(
                        p => ChangeViewModel((IPageViewModel)p),
                        p => p is IPageViewModel);
                }

                return _changePageCommand;
            }
        }

Solution

  • Fix you property repoPatch code:

    private string _repoPath;
    
            public string repoPath
            {
                get
                {
                    if (string.IsNullOrEmpty(_repoPath))
                    {
                        _repoPath= Properties.Settings.Default.repoPath;
                    }
                   return _repopath
                }
                ....................
                 ............