iam beginner in a mvvm and to learn it i wanted to build up some kind of application. So iam doing something like DB of my films, files and so on. I would like to have in an settings possibility to pick something like main folder of all files.. something like parent node.
For that iam using textbox with button [...] next to it. Textblock should be used just for showing the path to the file.
After program is started, it shows the default value inside of textBox, but after that iam unable to catch the change. FileInfo property gets changed, property to retrieve path out of it aswell. Also OnPropertyChanged() is called but checkbox stays still in its default state, no value change. Do you havge any idea why its not working for me? Thank you!
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);
}
}
}
SettingsView 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"/>
I think I understand what your question is. It just seems to not be updating the textbox when you change the file path. Here is how I do textbox binding for my applications.
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();
}
}
In the AddTubeView.xml
<TextBox x:Name="RowTextbox" Style="{StaticResource AddTubeTextbox}" PlaceholderText="Row #" Text="{Binding RowText, Mode=TwoWay}"/>
In this case I add the RaisePropertyChanged event to top of every class that implements INotifyPropertyChanged. It requires no arguments as it automatically takes the object that is calling it as the argument.
All I would have to do to make this update properly is just have any code in my VM do RowText = "NewText";
and this will automatically update in the View while the app is running.
Hope this helps! If I got the context wrong of your problem let me know and I will be happy to help :)