簡體   English   中英

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

[英]Usercontrol doesn`t update textbox 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>

這里的代碼存儲在用戶的配置中。 這樣他就不必在下一次開始時一次又一次地進入路徑。 小附加信息:由於config的路徑保存成功,第二次選擇路徑后測試框會更新。 這是因為第二次顯示先前保存的路徑。

視圖模型:

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
    }

這里還是喜歡在 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;
            }
        }

修復您的屬性 repoPatch 代碼:

private string _repoPath;

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

我發現了錯誤,問題是“repoPath”可以為空或空,而您只是在驗證它不為空。 但是如果“repoPath”的值為空,它總是會返回一個空值,因為該值不同於null。 您需要將驗證更改為

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

另外,我可以看到您將變量“_repoPath”的值保存到用戶設置“avlPath”而不是“repoPath”,是否正確?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM