用户控件不会更新文本框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;
            }
        }

解决方法

修正您的财产回购补丁代码:

private string _repoPath;

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

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

如果(!string.IsNullOrEmpty(Properties.Settings.Default.repoPath))

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...