【问题标题】:Usercontrol doesn`t update textbox mvvm WPF C#用户控件不更新文本框 mvvm WPF C#
【发布时间】:2020-11-04 18:27:12
【问题描述】:

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

我的问题: 正如您在此处看到的,有一个按钮可以选择计算机上的路径。 还有一个文本框再次显示路径。

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>

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

视图模型:

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

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

这里还是喜欢在MainWindowViewModel中用ICommand实现吧。

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

【问题讨论】:

    标签: c# wpf mvvm inotifypropertychanged


    【解决方案1】:

    修复你的属性 repoPatch 代码:

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-21
        • 1970-01-01
        • 1970-01-01
        • 2019-07-16
        相关资源
        最近更新 更多