2012-02-20 81 views
0

我创作自定义主题/控件时需要为用户选择.ico文件(Windows图标文件)的控件创建属性。它应该像一个选择窗体的背景属性。接受,这仅限于.ico文件。 到目前为止,我有这样的代码:C#创建自定义控件的文件属性浏览

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#自定义属性的文件选择?

回答

0

嗯,想通了,好像没有文件,但这是如何做到这一点:

private Icon IconLocation; 
public Icon CustomIcon 
{ 
    get 
    { 
     return IconLocation; 
    } 
    set 
    { 
     IconLocation = value; 
    } 
} 
1

我试图创建自己的UITypeEditor从UrlEditor衍生出ImgageUrlEditor并试图使用这一个。但是,这绝对没有影响。

[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; } 
} 

要么我缺少一些东西,要么这些滤镜属性在内部都没有使用。

+0

至少你尝试过:) – Annabelle 2012-02-21 01:56:16

+1

还请注意,我没有图标的网址分配给直接使用ViewState,因为ViewState在设计时不可用。相反,我只是将它存储在自动实现的属性ImageUrl中,并将其加载到Page_Load的Image控件中。 – 2012-02-21 13:48:04