/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context">A System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">The System.Globalization.CultureInfo to use as the current culture.</param> /// <param name="value">The System.Object to convert.</param> /// <returns> /// An <see cref="ActiveCommandCollection"/> that represents the converted value. /// </returns> /// <remarks> /// <para>This method will convert a string with format "<commandname>, ..." where <commandname> is the name of one of the supported commands.</para> /// </remarks> /// <example> /// <code> /// "OK, Cancel, Yes, No" /// </code> /// </example> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string AsString) { string[] StringList = AsString.Split(','); ActiveCommandCollection Result = new ActiveCommandCollection(); foreach (string Name in StringList) { ActiveCommandTypeConverter.TryParseName(Name.Trim(), (ActiveCommand command) => Result.Add(command)); } return(Result); } return(base.ConvertFrom(context, culture, value)); }
private void OnLoaded(object sender, RoutedEventArgs e) { ActiveCommandCollection ActiveCommands = ctrl.ActiveCommands; ActiveCommands.Clear(); ActiveCommands.Add(CustomControls.ActiveCommand.Ok); ActiveCommands.Add(CustomControls.ActiveCommand.Cancel); TypeConverter CollectionConverter = TypeDescriptor.GetConverter(ActiveCommands); Debug.Assert(CollectionConverter.CanConvertFrom(typeof(string))); Debug.Assert(!CollectionConverter.CanConvertFrom(typeof(int))); bool IsConvertedFrom; bool IsConvertedTo; ConvertActiveCommandCollection(CollectionConverter, "Ok", out IsConvertedFrom, ActiveCommands, out IsConvertedTo); Debug.Assert(IsConvertedFrom); Debug.Assert(IsConvertedTo); ConvertActiveCommandCollection(CollectionConverter, 0, out IsConvertedFrom, ActiveCommands, out IsConvertedTo); Debug.Assert(!IsConvertedFrom); Debug.Assert(!IsConvertedTo); TypeConverter Converter = TypeDescriptor.GetConverter(ActiveCommands[0]); Debug.Assert(Converter.CanConvertFrom(typeof(string))); Debug.Assert(!Converter.CanConvertFrom(typeof(int))); ConvertActiveCommand(Converter, "Ok", out IsConvertedFrom, ActiveCommands[0], out IsConvertedTo); Debug.Assert(IsConvertedFrom); Debug.Assert(IsConvertedTo); ConvertActiveCommand(Converter, 0, out IsConvertedFrom, ActiveCommands[0], out IsConvertedTo); Debug.Assert(!IsConvertedFrom); Debug.Assert(!IsConvertedTo); string SystemPath = Environment.GetFolderPath(Environment.SpecialFolder.System); string User32Path = Path.Combine(SystemPath, "user32.dll"); DialogValidation.LoadStringFromResourceFile("", 1); DialogValidation.LoadStringFromResourceFile(User32Path, 9999); }
/// <summary> /// Converts a <see cref="ActiveCommandCollection"/> to a string, using the specified context and culture information. /// </summary> /// <param name="context">A System.ComponentModel.ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">A System.Globalization.CultureInfo. If null is passed, the current culture is assumed.</param> /// <param name="value">The <see cref="ActiveCommandCollection"/> to convert.</param> /// <param name="destinationType">The System.Type to convert the value parameter to.</param> /// <returns> /// A string that represents the converted value. /// </returns> /// <remarks> /// <para>This method will convert to a string with format "<commandname>, ..." where <commandname> is the name of each command in the collection.</para> /// </remarks> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { ActiveCommandCollection ActiveCommandCollection = (ActiveCommandCollection)value; if (destinationType == typeof(string)) { string Result = string.Empty; foreach (ActiveCommand Command in ActiveCommandCollection) { if (Result.Length > 0) { Result += ","; } Result += Command.Name; } return(Result); } return(base.ConvertTo(context, culture, value, destinationType)); }
private void ConvertActiveCommandCollection(TypeConverter collectionConverter, object from, out bool isConvertedFrom, ActiveCommandCollection to, out bool isConvertedTo) { isConvertedFrom = true; try { collectionConverter.ConvertFrom(from); } catch { isConvertedFrom = false; } isConvertedTo = true; try { collectionConverter.ConvertTo(to, from.GetType()); } catch { isConvertedTo = false; } }