当我创作自定义主题/控件时,我需要为该控件创建一个属性,用户可以在其中选择.ico文件(Windows图标文件)。它应该像表单的Choose背景属性。接受,这仅限于.ico文件。到目前为止,我有这个代码:
private string IconLocation;
public string CustomIcon
{
get
{
return IconLocation;
}
set
{
IconLocation = value;
}
}
不管怎么说,这并没有像我想要的那样工作,我还发现了这个代码:
[DefaultValue(""), Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), UrlProperty, WebSysDescription("Image_ImageUrl"), Bindable(true), WebCategory("Appearance")]
public virtual string ImageUrl
{
get
{
string str = (string) this.ViewState["ImageUrl"];
if (str != null)
{
return str;
}
return string.Empty;
}
set
{
this.ViewState["ImageUrl"] = value;
}
}
来源:http://forums.asp.net/t/1335659.aspx
这也不起作用,因为视图状态不可用,所以我如何在普通c#中实现自定义属性的文件选择?
我试图像ImgageUrlEditor
那样从UrlEditor
派生一个UITypeEditor,但没有成功,并尝试使用这个UITypeEditor。然而,这绝对没有效果。
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public class IconUrlEditor : UrlEditor
{
protected override string Filter
{
get
{
return "Icon Files (*.ico)|*.ico";
}
}
}
我还设置了UrlProperty
的过滤属性。这似乎也没有什么效果。
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Image1.ImageUrl = ImageUrl;
}
[Editor(typeof(IconUrlEditor), typeof(UITypeEditor)), UrlProperty("*.ico")]
public virtual string ImageUrl { get; set; }
}
或者,我遗漏了一些内容,或者这些筛选器属性根本没有在内部使用。
嗯,弄清楚了,似乎没有文档,但这是如何做到的:
private Icon IconLocation;
public Icon CustomIcon
{
get
{
return IconLocation;
}
set
{
IconLocation = value;
}
}