如何将TextBox文本与ViewModel属性正确绑定

时间:2015-04-10 11:22:04

标签: c# wpf mvvm

我的观点上有一个文字框:

<TextBox x:Name="FilePath" Grid.Column="1" Height="30" Text="{Binding FilePath}"/>

在View Model中,我正在改变浏览按钮命令的路径:

 RelayCommand _browseButtonCommand;

    public ICommand BrowseButtonCommand
    {
        get
        {
            if (_browseButtonCommand == null)
            {
                _browseButtonCommand = new RelayCommand(param =>
                {
                    OpenFileDialog openFileDialog = new OpenFileDialog();

                    if ((openFileDialog.ShowDialog() == true))
                    {
                        FilePath = openFileDialog.FileName;
                    }

                });
            }
            return _browseButtonCommand;
        }
    }


string _filePath;

public string FilePath
        {
            get { return _filePath; }
            set { _filePath = value; OnPropertyChanged("_filePath"); }
        }

但是为什么更新的路径值没有出现在我的TextBox上?从Dialog !!中选择一个文件后,我能看到值正在改变。

1 个答案:

答案 0 :(得分:1)

您需要使用公共属性的名称发信号通知OnPropertyChanged,而不是私有字段的名称。

set { _filePath = value; OnPropertyChanged("FilePath"); }
相关问题