My PropertyGrid displays many string variables which are paths to files and folders,
I wouldn't want someone to edit them manually in textBox
is it possible to (set the read-only property field and) add a button "..." exactly the same as it appears in the Collection type
Like this:
Hi Mateusz,
Regards,
Gokul T
hello Gokul,
thank you so much for your help!
meanwhile I managed to read about custom editor and solved that by creating my BrowseFolderEditor,
your solution seems more elegant to me :)
my code:
public class BrowseFolderEditor : ITypeEditor
{
private Grid browseGrid;
private TextBox browseTextBox;
private Button browseButton;
public void Attach(PropertyViewItem property, PropertyItem info)
{
var binding = new Binding("Value")
{
Mode = BindingMode.TwoWay,
Source = info,
ValidatesOnExceptions = true,
ValidatesOnDataErrors = true
};
BindingOperations.SetBinding(browseTextBox, TextBox.TextProperty, binding);
}
public object Create(PropertyInfo propertyInfo)
{
browseGrid = new Grid();
var gridCol1 = new ColumnDefinition
{
Width = new GridLength(1, GridUnitType.Star)
};
browseGrid.ColumnDefinitions.Add(gridCol1);
var gridCol2 = new ColumnDefinition
{
Width = new GridLength(50, GridUnitType.Pixel)
};
browseGrid.ColumnDefinitions.Add(gridCol2);
browseTextBox = new TextBox()
{
IsReadOnly = true,
BorderThickness = new Thickness(0),
VerticalContentAlignment = VerticalAlignment.Center,
};
Grid.SetColumn(browseTextBox, 0);
browseButton = new Button
{
Content = "...",
};
browseButton.Click += BrowseFolder;
Grid.SetColumn(browseButton, 1);
browseGrid.Children.Add(browseTextBox);
browseGrid.Children.Add(browseButton);
return browseGrid;
}
public void Detach(PropertyViewItem property)
{
browseGrid = null;
browseTextBox = null;
browseButton = null;
}
private void BrowseFolder(object sender, RoutedEventArgs e)
{
using (var fbd = new FolderBrowserDialog())
{
var result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
browseTextBox.Text = fbd.SelectedPath;
browseTextBox.Focus(); //focus to refresh the value
}
}
}
}