PropertyGrid File chooser editor binding problem

Dec 26, 2012 at 9:53 AM

I'm creating a file chooser editor for PropertyGrid to allow set string property to an existing file using the code below. The user can edit the textbox directly or use the small button to open the file open dialog to set the textbox Text property to the selected file path.

Setting the textbox Text property to the FileOpen result path does not work well. The text box contains the selected path but it is not propagated to the object being edited by the property editor. Setting the textbox directly works without problems. Could you please help me?

Mirek.

 

public class FileChooserEditor : Xceed.Wpf.Toolkit.PropertyGrid.Editors.ITypeEditor {
	TextBox tb;

	public FrameworkElement ResolveEditor(Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem) {
		DockPanel dp = new DockPanel();
		dp.LastChildFill = true;
		Button bt = new Button();
		bt.Content = "...";
		bt.Click += new RoutedEventHandler(bt_Click);
		DockPanel.SetDock(bt, Dock.Right);
		dp.Children.Add(bt);
		tb = new TextBox();
		tb.Text = "xyz";
		dp.Children.Add(tb);

//create the binding from the bound property item to the editor var _binding = new Binding("Value"); //bind to the Value property of the PropertyItem _binding.Source = propertyItem; _binding.ValidatesOnExceptions = true; _binding.ValidatesOnDataErrors = true; _binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay; BindingOperations.SetBinding(tb, TextBox.TextProperty, _binding); return dp; } void bt_Click(Object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog openFile = new Microsoft.Win32.OpenFileDialog(); openFile.Filter = "Designer Files (*.xml)|*.xml|All Files (*.*)|*.*"; if (openFile.ShowDialog() == true) { tb.Text = openFile.FileName; } } }
Jan 30, 2013 at 11:21 AM

Hello,

have someone a solution of this behavior? I have the same issue and wish that it works

Thanks

Jan 30, 2013 at 3:08 PM

Hi,
add the two bold lines to the button click handler function:

void bt_Click(Object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog openFile = new Microsoft.Win32.OpenFileDialog(); setOpenFileDialog(openFile); if (openFile.ShowDialog() == true) { tb.Text = openFile.FileName; BindingExpression be = tb.GetBindingExpression(TextBox.TextProperty); be.UpdateSource(); } }
Mirek.

Jan 30, 2013 at 8:09 PM

Oh - fine

so simple - but it works

Thanks for your fast reply

CU