I am having another issue with WPF binding. Just when I think that everything worked out for me, I have more problems ...: S
Anyway ... I created a user control to select files. This is a simple text box followed by a button contained in a grid. The property of the control I'm working with is called FilePath, and the TextBox on this control is bound to this property. When the button is pressed, SaveFileDialog opens and the user selects the file. The user interface is updated correctly after the user selects a file.
The problem that I seem to encounter is that when I attach the object to the control (in this case I have an object with the DocumentFilePath property), the object does not update when a new file is selected.
Here is the relevant code in my user control:
public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register("FilePath", typeof(string), typeof(FileSave), new UIPropertyMetadata(string.Empty, OnFilePathChanged)); public string FilePath { get { return this.GetValue(FilePathProperty) as string; } set { this.SetValue(FilePathProperty, value); this.OnPropertyChanged("FilePath"); } } private void OnPropertyChanged(string propName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } private static void OnFilePathChanged(object sender, DependencyPropertyChangedEventArgs e) { ((FileSave)sender).OnPropertyChanged("FilePath"); }
And the user control is added to my window programmatically using reflection on my object:
private void AddFileSave(PropertyInfo pi) { FileSave fs = new FileSave(); Binding b = new Binding(pi.Name); fs.SetBinding(FileSave.FilePathProperty, b); this.AddToGrid(fs);
It may be worth noting that if I load a window with an existing object, my user control will be displayed correctly, but it still wonβt register any changes in the object to which it is attached.
Please let me know if you need more information.
Thanks in advance,
Sonny
EDIT: I found a way to solve this problem, but this is probably not a very good solution. Watching the debugger carefully, I found that when I set the FilePath property to my control, the object was unbound. If anyone can shed light on this, I will be very grateful. At the same time, I changed the code that my SaveFileDialog opens to look like this:
private void Button_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); ofd.Multiselect = false; ofd.Title = "Select document to import..."; ofd.ValidateNames = true; ofd.ShowDialog(); if (this.GetBindingExpression(FilePathProperty) == null) { this.FilePath = ofd.FileName; } else //set value on bound object (THIS IS THE NEW PORTION I JUST ADDED) { BindingExpression be = this.GetBindingExpression(FilePathProperty); string propName = be.ParentBinding.Path.Path; object entity = be.DataItem; System.Reflection.PropertyInfo pi = entity.GetType().GetProperty(propName); pi.SetValue(entity, ofd.FileName, null); } if (!string.IsNullOrWhiteSpace(this.FilePath)) { _fileContents = new MemoryStream(); using (StreamReader sr = new StreamReader(this.FilePath)) { _fileContents = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(sr.ReadToEnd())); } } else { _fileContents = null; } }