Beispiel #1
0
        internal static string GetDisplayStringForCulture(IEnumerable <Key> keys, ModifierKeys modifiers, ITypeDescriptorContext context, CultureInfo culture)
        {
            if (keys == null)
            {
                throw new ArgumentNullException(nameof(keys));
            }

            int numberOfKeys = keys.Count();

            if (numberOfKeys == 0 || (numberOfKeys == 1 && keys.First() == Key.None))
            {
                return(string.Empty);
            }

            var    result   = new StringBuilder();
            string firstKey = KeyConverter.ConvertTo(context, culture, keys.First(), typeof(string)) as string;

            if (!string.IsNullOrEmpty(firstKey))
            {
                string modifiersString = (string)ModifierKeysConverter.ConvertTo(context, culture, modifiers, typeof(string));
                bool   hasModifiers    = !string.IsNullOrEmpty(modifiersString);
                if (hasModifiers)
                {
                    result.Append(modifiersString);     // result = "Ctrl+Shift"
                    result.Append(ModifiersDelimiter);  // result = "Ctrl+Shift+"
                }

                result.Append(firstKey);                // result = "Ctrl+Shift+A"

                for (int i = 1; i < numberOfKeys; i++)
                {
                    string key = KeyConverter.ConvertTo(context, culture, keys.ElementAt(i), typeof(string)) as string;
                    if (!string.IsNullOrEmpty(key))
                    {
                        result.Append(KeysSeparator);   // result = "Ctrl+Shift+A,"
                        result.Append(' ');             // result = "Ctrl+Shift+A, "

                        if (hasModifiers)
                        {
                            result.Append(modifiersString);     // result = "Ctrl+Shift+A, Ctrl+Shift"
                            result.Append(ModifiersDelimiter);  // result = "Ctrl+Shift+A, Ctrl+Shift+"
                        }

                        result.Append(key);             // result = "Ctrl+Shift+A, Ctrl+Shift+B"
                    }
                }
            }

            return(result.ToString());
        }
Beispiel #2
0
        /// <inheritdoc />
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException(nameof(destinationType));
            }

            if (destinationType == typeof(string))
            {
                if (value != null)
                {
                    if (value is KeyGestureEx keyGesture)
                    {
                        if (keyGesture.Key == Key.None)
                        {
                            return(string.Empty);
                        }

                        var strBinding = string.Empty;
                        var strKey     = (string)keyConverter.ConvertTo(context, culture, keyGesture.Key, destinationType);
                        if (strKey != string.Empty)
                        {
                            strBinding += modifierKeysConverter.ConvertTo(context, culture, keyGesture.Modifiers, destinationType) as string;
                            if (strBinding != string.Empty)
                            {
                                strBinding += MODIFIERS_DELIMITER;
                            }

                            strBinding += strKey;

                            if (!string.IsNullOrEmpty(keyGesture.DisplayString))
                            {
                                strBinding += DISPLAYSTRING_SEPARATOR + keyGesture.DisplayString;
                            }
                        }

                        return(strBinding);
                    }
                }
                else
                {
                    return(string.Empty);
                }
            }

            throw this.GetConvertToException(value, destinationType);
        }
        /// <summary>
        ///   Converts the given value object to the specified type, using the specified context and culture information.
        /// </summary>
        /// <param name="context"> An <see cref="System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
        /// <param name="culture"> A <see cref="System.Globalization.CultureInfo" /> . If null is passed, the current culture is assumed. </param>
        /// <param name="value"> The <see cref="object" /> to convert. </param>
        /// <param name="destinationType"> The <see cref="System.Type" /> to convert the <paramref name="value" /> parameter to. </param>
        /// <returns> An <see cref="object" /> that represents the converted value. </returns>
        /// <exception cref="System.ArgumentNullException">The
        ///   <paramref name="destinationType" />
        ///   parameter is null.</exception>
        /// <exception cref="System.NotSupportedException">The conversion cannot be performed.</exception>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                var gesture = value as MultiKeyGesture;

                if (gesture != null)
                {
                    var builder = new StringBuilder();

                    KeySequence sequence;

                    for (var i = 0; i < gesture.KeySequences.Length; i++)
                    {
                        if (i > 0)
                        {
                            builder.Append(", ");
                        }

                        sequence = gesture.KeySequences[i];
                        if (sequence.Modifiers != ModifierKeys.None)
                        {
                            builder.Append((string)modifierKeysConverter.ConvertTo(context, culture, sequence.Modifiers, destinationType));
                            builder.Append("+");
                        }

                        builder.Append((string)keyConverter.ConvertTo(context, culture, sequence.Keys[0], destinationType));

                        for (var j = 1; j < sequence.Keys.Length; j++)
                        {
                            builder.Append("+");
                            builder.Append((string)keyConverter.ConvertTo(context, culture, sequence.Keys[0], destinationType));
                        }
                    }

                    return(builder.ToString());
                }
            }

            throw GetConvertToException(value, destinationType);
        }
 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 {
     if (destinationType == null)
     {
         throw new ArgumentNullException("destinationType");
     }
     if (destinationType == typeof(string))
     {
         if (value == null)
         {
             return(string.Empty);
         }
         UnrestrictedKeyGesture gesture = value as UnrestrictedKeyGesture;
         if (gesture != null)
         {
             if (gesture.Key == Key.None)
             {
                 return(string.Empty);
             }
             string str  = "";
             string str2 = (string)keyConverter.ConvertTo(context, culture, gesture.Key, destinationType);
             if (str2 != string.Empty)
             {
                 str = str + (modifierKeysConverter.ConvertTo(context, culture, gesture.Modifiers, destinationType) as string);
                 if (str != string.Empty)
                 {
                     str = str + '+';
                 }
                 str = str + str2;
                 if (!string.IsNullOrEmpty(gesture.DisplayString))
                 {
                     str = str + ',' + gesture.DisplayString;
                 }
             }
             return(str);
         }
     }
     throw base.GetConvertToException(value, destinationType);
 }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                if (value == null)
                {
                    return(String.Empty);
                }

                if (value is KeyCombination combination)
                {
                    if (combination.Equals(KeyCombination.None))
                    {
                        return(String.Empty);
                    }
                    // convert key and modifiers to string
                    string keyString = keyConv.ConvertTo(context, culture, combination.Key, destinationType) as string;
                    string modString = modifiersConv.ConvertTo(context, culture, combination.Modifiers, destinationType) as string;
                    return(modString + DELIMITER + keyString);
                }
            }
            throw GetConvertToException(value, destinationType);
        }
        protected virtual string ConvertTo(ModifierKeys value, Type type = null)
        {
            ModifierKeysConverter c = new ModifierKeysConverter();

            return((string)c.ConvertTo(value, type ?? typeof(string)));
        }