我正在使用一个带有大约100个不同图标的图像列表,这些图标将在一个使用Winforms的C#项目的列表视图中使用
图像由listview通过它们的索引来引用。
现在已经有了一个图形大修,所有的图标都需要更换。到目前为止,我所做的是在Visual Studio编辑器中打开图像列表,删除索引为5的图像x,然后添加图像x的新版本。

这种方法的问题是,当删除图像x时,索引高于5的所有其他图像都会被移位。在添加新版本的图标后,我必须单击向上箭头大约95次才能再次获得索引5处的新版本。
有没有人知道一种不那么痛苦,更不容易出错的方法来达到同样的结果?
编辑.designer.cs文件并没有真正的帮助,因为它只列出了图标的名称及其索引
//
// NavigatorImageList
//
this.NavigatorImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("NavigatorImageList.ImageStream")));
this.NavigatorImageList.TransparentColor = System.Drawing.Color.Transparent;
this.NavigatorImageList.Images.SetKeyName(0, "dummy.png");
this.NavigatorImageList.Images.SetKeyName(1, "Attribute.png");
this.NavigatorImageList.Images.SetKeyName(2, "Operation.png");
this.NavigatorImageList.Images.SetKeyName(3, "Element.png");
this.NavigatorImageList.Images.SetKeyName(4, "Diagram.png");
this.NavigatorImageList.Images.SetKeyName(5, "Package_element.png");
// ...continues like this for all items ....实际的图像存储在.resx文件中,但它们是二进制格式的,因此也不能在那里编辑它们
<data name="NavigatorImageList.ImageStream" mimetype="application/x-microsoft.net.object.binary.base64">
<value>
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAAB2
1gAAAk1TRnQBSQFMAgEBYwEAAYgBAgGIAQIBEAEAARABAAT/AREBAAj/AUIBTQE2BwABNgMAASgDAAFA
AwABkAEBAgABAQEAARAGAAHIJgABeAEtAfABHAHwARwB8AEcAfABHAHwARwWAAG4ATUBMAElATABJQEw
... and many many more lines like this...我知道通过索引来引用图像可能是一个错误,但现在改变这一点为时已晚
TL;DR
我正在寻找一种简单的方法来替换我的imagelist中的图像,而不必更改依赖于图像索引的现有工作代码。
发布于 2019-12-04 18:30:35
图像列表编辑器还有一个Image属性,该属性默认情况下是不可浏览的。作为一种选择,您可以使该属性可见,然后您可以轻松地在设计时替换图像,而不会出现任何问题。
查看下图并查看Image属性:

这是我的图像集合编辑器。它基本上是原始Image Collection编辑器的代码,只做了一点小改动,找到了集合编辑器的PropertyGrid并重置了它的BrowsableAttributes属性。
public class MyImageListEditor : CollectionEditor
{
Type ImageListImageType;
public MyImageListEditor(Type type) : base(type)
{
ImageListImageType = typeof(ControlDesigner).Assembly
.GetType("System.Windows.Forms.Design.ImageListImage");
}
protected override string GetDisplayText(object value)
{
if (value == null)
return string.Empty;
PropertyDescriptor property = TypeDescriptor.GetProperties(value)["Name"];
if (property != null)
{
string str = (string)property.GetValue(value);
if (str != null && str.Length > 0)
return str;
}
if (value.GetType() == ImageListImageType)
value = (object)((dynamic)value).Image;
string name = TypeDescriptor.GetConverter(value).ConvertToString(value);
if (name == null || name.Length == 0)
name = value.GetType().Name;
return name;
}
protected override object CreateInstance(Type type)
{
return ((UITypeEditor)TypeDescriptor.GetEditor(ImageListImageType,
typeof(UITypeEditor))).EditValue(Context, null);
}
protected override CollectionEditor.CollectionForm CreateCollectionForm()
{
CollectionEditor.CollectionForm collectionForm = base.CreateCollectionForm();
collectionForm.Text = "My Image Collection Editor";
var overArchingTableLayoutPanel =
(TableLayoutPanel)collectionForm.Controls["overArchingTableLayoutPanel"];
var propertyBrowser =
(PropertyGrid)overArchingTableLayoutPanel.Controls["propertyBrowser"];
propertyBrowser.BrowsableAttributes = new AttributeCollection();
return collectionForm;
}
protected override IList GetObjectsFromInstance(object instance)
{
return (IList)(instance as ArrayList) ?? (IList)null;
}
}要为图像列表注册此编辑器,最简单的解决方案是在基类的构造函数中注册它:
public partial class Form1 : MyBaseForm
{
public Form1()
{
InitializeComponent();
}
}
public class MyBaseForm : Form
{
public MyBaseForm()
{
TypeDescriptor.AddAttributes(typeof(ImageList.ImageCollection),
new Attribute[] {
new EditorAttribute(typeof(MyImageListEditor), typeof(UITypeEditor)) });
}
}然后关闭所有设计器,重新构建解决方案,关闭并重新打开VS。
就这样!
https://stackoverflow.com/questions/58909020
复制相似问题