如何在 TextBox 上刷新成员 属性 的值 - mvvm

How to refresh value of member property on a TextBox - mvvm

我是 mvvm 的初学者,为了学习它,我想构建某种应用程序。所以我正在做一些像我的电影、文件等的数据库。我想在设置中选择所有文件的主文件夹之类的东西.. 父节点之类的东西。

为此,我使用旁边带有按钮 [...] 的文本框。 文本块应该仅用于显示文件路径。

程序启动后,显示textBox内的默认值,但之后我无法捕捉到变化。 FileInfo 属性 已更改,属性 也从中检索路径。还调用了 OnPropertyChanged() 但复选框仍保持其默认状态,没有值更改。你知道为什么它对我不起作用吗?谢谢!

class SettingsViewModel : ObservableObject, IPageViewModel
{
    private AppSettings settings;

    public AppSettings Settings { get => settings;}

       public SettingsViewModel()
            {
                settings = new AppSettings();
            }
       private void ChangeFilmsFolderButton_Click(object obj)
            {
                string name = Settings.GeneralFilmsFolderPath; //old value
                Settings.AddPathToFilmsFolder();
                name = Settings.GeneralFilmsFolderPath; //new value
            }
    }

    class AppSettings : ObservableObject
    {   
            private FileInfo _generalFilmsFolder;
            private string _generalFilmsFolderPath;

            public FileInfo GeneralFilmsFolder
            {
                get => _generalFilmsFolder;
                set
                {
                    _generalFilmsFolder = value;
                    GeneralFilmsFolderPath = _generalFilmsFolder.FullName;
                }
            }

            public string GeneralFilmsFolderPath {
                get => _generalFilmsFolderPath;
                set {
                    _generalFilmsFolderPath = value;
                    OnPropertyChanged("GeneralFilmsFolderPath");
                }
            }


            public void AddPathToFilmsFolder()
            {
                CommonOpenFileDialog dialog = new CommonOpenFileDialog();
                dialog.InitialDirectory = "C:\Users";
                dialog.IsFolderPicker = true;
                dialog.Multiselect = false;

                if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
                {
                    if (Directory.Exists(dialog.FileName))
                        GeneralFilmsFolder = new FileInfo(dialog.FileName);
                }
            }

    }

设置视图XAML

<TextBox IsReadOnly="True" HorizontalAlignment="Left" Height="20" Margin="74,2,0,0" TextWrapping="Wrap" Text="{Binding Settings.GeneralFilmsFolderPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="73"/>

我想我明白你的问题了。当您更改文件路径时,它似乎不会更新文本框。下面是我如何为我的应用程序绑定文本框。

    public class AddTubeViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// Raised when a property on this object has a new value.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises this object's PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">The property that has a new value.</param>
        private void RaisePropertyChanged([CallerMemberName]string propertyName = "")
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private string _rowText;
        /// <summary>
        /// The text value of the row textbox
        /// </summary>
        public string RowText
        {
            get { return _rowText; }
            set
            {
                _rowText = value;
                RaisePropertyChanged();
            }
        }

在AddTubeView.xml

<TextBox x:Name="RowTextbox" Style="{StaticResource AddTubeTextbox}" PlaceholderText="Row #" Text="{Binding RowText, Mode=TwoWay}"/>

在这种情况下,我将 RaisePropertyChanged 事件添加到每个实现 INotifyPropertyChanged 的​​ class 的顶部。它不需要任何参数,因为它会自动将调用它的对象作为参数。

要正确进行此更新,我只需在我的 VM 中执行任何代码 RowText = "NewText";,这将在应用处于 运行 时在视图中自动更新。

希望对您有所帮助!如果我弄错了你问题的上下文,请告诉我,我很乐意提供帮助:)