2014-01-07 22 views
0

我有從StringConvertor類繼承videoformates類的哈希表。有兩個功能ConvertFromConvertTo覆蓋那裏。這兩個函數如何將視頻合成器顯示爲字符串。propertyGrid與字符串轉換器中的hastable繼承類

public class VideoFormate : StringConverter 
{ 
    private Hashtable VideoFormates; 
    public VideoFormate() { 
      VideoFormates = new Hashtable(); 
      VideoFormates[".3g2"] = "3GPP2"; 
      VideoFormates[".3gp"] = "3GPP"; 
    } 
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) 
    { 
     return true; 
    } 

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) 
    { 
     return new StandardValuesCollection(VideoFormates); 
    } 
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) 
    { 
     return true; 
    } 
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
    { 
     return base.ConvertTo(context, culture, value, destinationType); 
    } 
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
    { 
     return base.ConvertFrom(context, culture, value); 
    } 

類視頻屬性是

class videoProperties 
{ 
    private string _VideoFormat; 

    [TypeConverter(typeof(VideoFormate)), 
    CategoryAttribute("Video Setting"), 
    DefaultValueAttribute(0), 
    DescriptionAttribute("Select a Formate from the list")] 
    public string VideoFormat 
    { 
     get { return _VideoFormat; } 
     set { _VideoFormat = value; } 
    } 

} 

我想顯示3GPP2作爲顯示部件和.3gp2如在PropertyGrid中組合框中值構件。

+0

1)這是.NET 1.1嗎? 2)理想的轉換應該如何?用戶應該看到什麼值 - 「.3g2」或「3GPP2」? 'videoProperties.VideoFormat'應該包含什麼值? – Dennis

+0

泰克你問。 .NET 2,用戶看起來像「3GPP2」,並且'videoProperties.VideoFormat'中包含的值是.3gp2 – Abubakar

回答

0

調試完代碼後,我找到了答案。

首次運行時,在上面的代碼中,propertyGrid下拉列表填充Hashtable集合。 ConvertTo方法將其轉換爲字符串。所以它保存爲System.Collections.DictionaryEntry。 當從組合框中選擇一個元素時,它將videoProperties.VideoFormat設置爲System.Collections.DictionaryEntry。所以我改變了ConvertTo函數如下。

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
    { 
     if (value is DictionaryEntry) 
     { 
      return ((DictionaryEntry)value).Key; 
     } 
     else if (value != null) 
     { 
      return VideoFormates[value]; 
     } 
     return base.ConvertTo(context, culture, value, destinationType); 
    } 

如果功能檢查值類型是DictionaryEntry然後將它轉換並返回value參數的key元件。所以組合框填寫key值。當窗體顯示ConvertTo函數再次調用,第二次返回組合框值。那麼simplay基於組合框值返回Hashtable值。