| | 1 | | using System; |
| | 2 | | using System.ComponentModel; |
| | 3 | |
|
| | 4 | | namespace PropertyGridHelpers.Attributes |
| | 5 | | { |
| | 6 | | #if NET8_0_OR_GREATER |
| | 7 | | /// <summary> |
| | 8 | | /// Attribute for specifying a localized category name for a property or event. |
| | 9 | | /// </summary> |
| | 10 | | /// <param name="resourceKey">The key used to retrieve the localized category name from the resource file.</param> |
| | 11 | | /// <remarks> |
| | 12 | | /// This attribute allows category names displayed in property grids to be localized |
| | 13 | | /// by retrieving the category name from a resource file. If your class has a <see cref="ResourcePathAttribute" /> |
| | 14 | | /// applied to it, the resource key will be looked up in the resource file specified by that attribute. |
| | 15 | | /// </remarks> |
| | 16 | | /// <example> |
| | 17 | | /// <code language="csharp"> |
| | 18 | | /// [ResourcePath(nameof(TestControl))] |
| | 19 | | /// [TypeDescriptionProvider(typeof(LocalizedTypeDescriptionProvider))] |
| | 20 | | /// public partial class TestControl : UserControl |
| | 21 | | /// { |
| | 22 | | /// [LocalizedCategory("Category_Layout")] |
| | 23 | | /// [LocalizedDescription("Description_Scrollbar")] |
| | 24 | | /// [LocalizedDisplayName("DisplayName_Scrollbar")] |
| | 25 | | /// [Editor(typeof(FlagEnumUIEditor<EnumTextConverter<ScrollBars>>), typeof(UITypeEditor))] |
| | 26 | | /// [TypeConverter(typeof(EnumTextConverter<ScrollBars>))] |
| | 27 | | /// [DefaultValue(ScrollBars.None)] |
| | 28 | | /// [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] |
| | 29 | | /// [EditorBrowsable(EditorBrowsableState.Always)] |
| | 30 | | /// [Bindable(true)] |
| | 31 | | /// public ScrollBars Scrollbars |
| | 32 | | /// { |
| | 33 | | /// get => _Scrollbars; |
| | 34 | | /// set => _Scrollbars = value; |
| | 35 | | /// } |
| | 36 | | /// } |
| | 37 | | /// </code> |
| | 38 | | /// </example> |
| | 39 | | /// <seealso cref="LocalizedTextAttribute" /> |
| | 40 | | [AttributeUsage(AttributeTargets.Property | AttributeTargets.Event | AttributeTargets.Method, AllowMultiple = false) |
| | 41 | | public class LocalizedCategoryAttribute(string resourceKey) : |
| 32 | 42 | | LocalizedTextAttribute(resourceKey) |
| | 43 | | { |
| | 44 | | #else |
| | 45 | | /// <summary> |
| | 46 | | /// Attribute for specifying a localized category name for a property or event. |
| | 47 | | /// </summary> |
| | 48 | | /// <remarks> |
| | 49 | | /// This attribute allows category names displayed in property grids to be localized |
| | 50 | | /// by retrieving the category name from a resource file. If your class has a <see cref="ResourcePathAttribute" /> |
| | 51 | | /// applied to it, the resource key will be looked up in the resource file specified by that attribute. |
| | 52 | | /// </remarks> |
| | 53 | | /// <seealso cref="LocalizedTextAttribute" /> |
| | 54 | | [AttributeUsage(AttributeTargets.Property | AttributeTargets.Event | AttributeTargets.Method, AllowMultiple = false) |
| | 55 | | public class LocalizedCategoryAttribute |
| | 56 | | : LocalizedTextAttribute |
| | 57 | | { |
| | 58 | | /// <summary> |
| | 59 | | /// Initializes a new instance of the <see cref="LocalizedCategoryAttribute" /> class. |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="resourceKey">The key used to retrieve the localized category name from the resource file.</para |
| | 62 | | /// <remarks> |
| | 63 | | /// The constructor fetches the localized category name using the specified resource key. |
| | 64 | | /// </remarks> |
| | 65 | | /// <example> |
| | 66 | | /// <code language="csharp"> |
| | 67 | | /// [ResourcePath(nameof(TestControl))] |
| | 68 | | /// [TypeDescriptionProvider(typeof(LocalizedTypeDescriptionProvider))] |
| | 69 | | /// public partial class TestControl : UserControl |
| | 70 | | /// { |
| | 71 | | /// [LocalizedCategory("Category_Layout")] |
| | 72 | | /// [LocalizedDescription("Description_Scrollbar")] |
| | 73 | | /// [LocalizedDisplayName("DisplayName_Scrollbar")] |
| | 74 | | /// [Editor(typeof(FlagEnumUIEditor<EnumTextConverter<ScrollBars>>), typeof(UITypeEditor)) |
| | 75 | | /// [TypeConverter(typeof(EnumTextConverter<ScrollBars>))] |
| | 76 | | /// [DefaultValue(ScrollBars.None)] |
| | 77 | | /// [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] |
| | 78 | | /// [EditorBrowsable(EditorBrowsableState.Always)] |
| | 79 | | /// [Bindable(true)] |
| | 80 | | /// public ScrollBars Scrollbars |
| | 81 | | /// { |
| | 82 | | /// get => _Scrollbars; |
| | 83 | | /// set => _Scrollbars = value; |
| | 84 | | /// } |
| | 85 | | /// } |
| | 86 | | /// </code> |
| | 87 | | /// </example> |
| | 88 | | public LocalizedCategoryAttribute(string resourceKey) |
| 16 | 89 | | : base(resourceKey) |
| 8 | 90 | | { |
| 16 | 91 | | } |
| | 92 | | #endif |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// Gets the <see cref="LocalizedCategoryAttribute"/> from the specified context. |
| | 96 | | /// </summary> |
| | 97 | | /// <param name="context">The context.</param> |
| | 98 | | /// <returns></returns> |
| | 99 | | public static new LocalizedCategoryAttribute Get(ITypeDescriptorContext context) => |
| 28 | 100 | | context == null || context.Instance == null || context.PropertyDescriptor == null |
| 28 | 101 | | ? null |
| 28 | 102 | | : Support.Support.GetFirstCustomAttribute<LocalizedCategoryAttribute>( |
| 28 | 103 | | Support.Support.GetPropertyInfo(context)); |
| | 104 | | } |
| | 105 | | } |