用户控件不更新文本框 mvvm WPF C#

Usercontrol doesn`t update textbox mvvm WPF C#

抱歉代码太多。 但不幸的是,有太多的联系。 这就是为什么我不得不插入这么多代码。 我试图将它保持在最低限度。 希望够了。

关于我的问题: 正如您在这里看到的那样,有一个按钮可以 select 计算机上的路径。 还有一个文本框再次显示路径。

XAML 用户控制代码:

<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>

此处代码存储在用户的配置中。 使他不必在下一次开始时一次又一次地进入路径。 小附加信息: 由于配置路径已成功保存,测试框将在您第二次 select 路径后更新。 这是因为第二次显示之前保存的路径。

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();
    }
}

这是我继承自 INotifyPropertyChanged 的​​ ObservableObject。

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);
            }
        }
}

这是我继承自 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
    }

这里还是喜欢用ICommand实现的MainWindowViewModel。

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;
            }
        }

我发现了错误,问题是“repoPath”可以为 null 或为空,而您只是验证它不为 null。但如果“repoPath”的值为空,它将始终 return 一个空值,因为该值不同于 null。您需要将验证更改为

if (!string.IsNullOrEmpty(Properties.Settings.Default.repoPath))

另外,我看到您将变量“_repoPath”的值保存到用户设置“avlPath”而不是“repoPath”,是否正确?

修复你属性 repo补丁码:

private string _repoPath;

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