【发布时间】:2020-07-19 11:28:20
【问题描述】:
C#、WPF、xceed PropertyGrid。我正在使用自定义控件在PropertyGrid 中提供浏览按钮。用例存在差异(例如,最明显的是浏览文件夹与文件),为这些用例创建单独的编辑器不会很干。理想情况下,我想引入一个参数,但我不确定如何将它传递给控件。有没有一种相当简单的方法来实现这一点?
对我来说,最优雅的解决方案似乎能够将枚举传递给它(用于“模式”),但如果我可以获得编辑器附加到的属性(即以下示例中的 ProjectFolder),那么这也可以达到目的。
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;
}
}
}
它是这样使用的:
[Editor(typeof(MyControls.PropertyGridFilePicker), typeof(MyControls.PropertyGridFilePicker))]
public string ProjectFolder { get; set; } = "";
【问题讨论】:
标签: c# wpf propertygrid xceed