C#
, WPF
, xceed PropertyGrid
. I am using a custom control to provide a browse button in a PropertyGrid
. There are variations in use case (e.g. most obviously browsing for a folder vs file), and creating separate editors for those cases would not be very DRY. Ideally I would like to introduce a parameter, but I am not sure how to pass that to the control. Is there a reasonably simple way to achieve this?
To me the most elegant solution would seem to be able to pass it an enum (for 'mode'), but if I could get the property that the editor is attached to (i.e. ProjectFolder
in the following example) then that would also serve the purpose.
public partial class PropertyGridFilePicker : ITypeEditor
{
string rtn = "";
public PropertyGridFilePicker()
{
InitializeComponent();
}
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
// Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(string), typeof(PropertyGridFilePicker), new PropertyMetadata(null));
public FrameworkElement ResolveEditor(PropertyItem propertyItem)
{
Binding binding = new Binding("Value");
binding.Source = propertyItem;
binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
BindingOperations.SetBinding(this, ValueProperty, binding);
return this;
}
private void PickFileButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == true && fd.CheckFileExists)
{
Value = fd.FileName;
Value = rtn;
}
}
}
It is used like this:
[Editor(typeof(MyControls.PropertyGridFilePicker), typeof(MyControls.PropertyGridFilePicker))]
public string ProjectFolder { get; set; } = "";
Answer given here:
Possible to call the constructor of a WPF type editor (inheriting from ITypeEditor)?
Although posted as a different question, this related to the same problem. I asked for clarification on the Dependency Injection solution proposed in the other answer given here since I didn't understand how this could work. And it seems it wouldn't.