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

问题描述

对不起,这么多代码。 但是不幸的是有如此多的联系。 这就是为什么我必须插入太多代码的原因。 我试图将其降至最低。 我希望这够了。

对于我的问题: 如您所见,这里有一个按钮可以选择计算机上的路径。 还有一个文本框再次显示路径。

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>

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

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

这是我的ObservableObject,它继承自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);
            }
        }
}

这是我的RelayCommand,它是从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;
            }
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)