- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 PropertyGrid 编辑包含集合的对象。使用 CollectionEditor 编辑集合。我必须确保集合中的元素是唯一的。
如何向 CollectionEditor 添加验证:
最佳答案
您可以创建自己的集合编辑器,并挂接到默认编辑器控件上的事件。您可以使用这些事件来禁用 OK 按钮。像这样的东西:
public class MyCollectionEditor : CollectionEditor
{
private static Dictionary<CollectionForm, Button> okayButtons
= new Dictionary<CollectionForm, Button>();
// Inherit the default constructor from CollectionEditor
public MyCollectionEditor(Type type)
: base(type)
{
}
// Override this method in order to access the containing user controls
// from the default Collection Editor form or to add new ones...
protected override CollectionForm CreateCollectionForm()
{
CollectionForm collectionForm = base.CreateCollectionForm();
collectionForm.FormClosed +=
new FormClosedEventHandler(collectionForm_FormClosed);
collectionForm.Load += new EventHandler(collectionForm_Load);
if (collectionForm.Controls.Count > 0)
{
TableLayoutPanel mainPanel = collectionForm.Controls[0]
as TableLayoutPanel;
if ((mainPanel != null) && (mainPanel.Controls.Count > 7))
{
// Get a reference to the inner PropertyGrid and hook
// an event handler to it.
PropertyGrid propertyGrid = mainPanel.Controls[5]
as PropertyGrid;
if (propertyGrid != null)
{
propertyGrid.PropertyValueChanged +=
new PropertyValueChangedEventHandler(
propertyGrid_PropertyValueChanged);
}
// Also hook to the Add/Remove
TableLayoutPanel buttonPanel = mainPanel.Controls[1]
as TableLayoutPanel;
if ((buttonPanel != null) && (buttonPanel.Controls.Count > 1))
{
Button addButton = buttonPanel.Controls[0] as Button;
if (addButton != null)
{
addButton.Click += new EventHandler(addButton_Click);
}
Button removeButton = buttonPanel.Controls[1] as Button;
if (removeButton != null)
{
removeButton.Click +=
new EventHandler(removeButton_Click);
}
}
// Find the OK button, and hold onto it.
buttonPanel = mainPanel.Controls[6] as TableLayoutPanel;
if ((buttonPanel != null) && (buttonPanel.Controls.Count > 1))
{
Button okayButton = buttonPanel.Controls[0] as Button;
if (okayButton != null)
{
okayButtons[collectionForm] = okayButton;
}
}
}
}
return collectionForm;
}
private static void collectionForm_FormClosed(object sender,
FormClosedEventArgs e)
{
CollectionForm collectionForm = (CollectionForm)sender;
if (okayButtons.ContainsKey(collectionForm))
{
okayButtons.Remove(collectionForm);
}
}
private static void collectionForm_Load(object sender, EventArgs e)
{
ValidateEditValue((CollectionForm)sender);
}
private static void propertyGrid_PropertyValueChanged(object sender,
PropertyValueChangedEventArgs e)
{
ValidateEditValue((CollectionForm)sender);
}
private static void addButton_Click(object sender, EventArgs e)
{
Button addButton = (Button)sender;
ValidateEditValue((CollectionForm)addButton.Parent.Parent.Parent);
}
private static void removeButton_Click(object sender, EventArgs e)
{
Button removeButton = (Button)sender;
ValidateEditValue((CollectionForm)removeButton.Parent.Parent.Parent);
}
private static void ValidateEditValue(CollectionForm collectionForm)
{
if (okayButtons.ContainsKey(collectionForm))
{
Button okayButton = okayButtons[collectionForm];
IList<MyClass> items = collectionForm.EditValue as IList<MyClass>;
okayButton.Enabled = MyCollectionIsValid(items);
}
}
private static bool MyCollectionIsValid(IList<MyClass> items)
{
// Perform validation here.
return (items.Count == 2);
}
}
您还需要为您的集合添加一个编辑器属性:
class MyClass
{
[Editor(typeof(MyCollectionEditor),
typeof(System.Drawing.Design.UITypeEditor))]
List<Foo> MyCollection
{
get; set;
}
}
注意:我发现 removeButton_Click 中项目的值不正确 - 因此可能需要进行一些调整。
关于c# - 如何向 PropertyGrid 的 CollectionEditor 添加验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3497180/
我正在用可以让我更好地自定义 UI 的东西替换我的属性网格。我在我的表单上放置了一个按钮,我希望点击它时会弹出一个 CollectionEditor 并允许我修改我的代码。当我使用 PropertyG
我有这样的属性: [Editor(typeof(LayerCollection), typeof(UITypeEditor))] public List Layers { get { return l
请先阅读整个问题以了解在哪里我可以重置属性的默认值。 当定义一个可以可视化设计的自定义类时,可以使用以下模式实现一个集合编辑器来修改列表、数组、集合等属性: [Editor(typeof(Collec
谁能用通俗易懂的语言告诉我CollectionEditor的内部工作机制? 我已经实现了每一个虚函数并且进入了每一个。仍然不知道其预期算法。我在网上搜索了很多关于 CollectionEditor 类
我在 WinForms 中使用 PropertyGrid。我可以更改默认的 CollectionEditor 吗?我可以通过 Editor 属性显示自定义 CollectionEditor,如下所示:
我有一个组件,其中有 List属性(property)。列表中的类的每个属性都用描述属性修饰,但描述不会显示在集合编辑器中 在 IDE 设计器中,有没有办法打开标准集合编辑器中的“描述”面板?我需要从
我正在使用 PropertyGrid 编辑包含集合的对象。使用 CollectionEditor 编辑集合。我必须确保集合中的元素是唯一的。 如何向 CollectionEditor 添加验证: 通过
我有一个用户控件,它有一个属性,该属性是自定义对象类型的列表。当我通过从 List<> 继承来创建自定义类时,它主要起作用: public class CustomCol : List {
为了设计时支持,我正在实现一个 CollectionEditor,如下所示: class CustomCollectionEditor : CollectionEditor { ...
我有很久以前从某个地方抓取的这个自定义控件: public class NotifyingCollectionEditor : CollectionEditor { // Define a s
我已经创建了自定义控件。它有一个名为“Tab”的属性。此属性将从“DockContainerItem”类继承的“FloorsInformation”控件集合添加到我的自定义控件。 现在,我想在单击选项
我有一个用户控件,它使用 CollectionEditor 的属性。我正在同一解决方案中使用另一个项目来测试控件。我的集合编辑器没有问题,除了在我重新编译组件 dll 后 IDE 在设计时给出错误。如
使用 Extended WPF Toolkit 中的 PropertyGrid,我需要使用 CollectionEditor: [Editor(typeof(CollectionEditor), ty
我有一个自定义控件,它的属性类型为 Collection .当我使用 CollectionEditor要编辑此属性,CollectionEditor窗口显示"Object does not match
我是一名优秀的程序员,十分优秀!