Simsons
Simsons

Reputation: 12745

How do I properly bind TextBox Text with ViewModel Property

I have a text Box on My View:

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

In View Model I am changing the Path on Browse Button Command:

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

But why the Updated path value is not appearing on my TextBox? I am able to see value is changing after I select a File from Dialog!!

Upvotes: 1

Views: 1225

Answers (1)

Udontknow
Udontknow

Reputation: 1580

You need to signal OnPropertyChanged with the name of the public property, not the name of the private field.

set { _filePath = value; OnPropertyChanged("FilePath"); }

Upvotes: 1

Related Questions