Forms-Eigenschaftenfenster: Platzieren einer benutzerdefinierten Eigenschaft in eine SubCategory

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von VB1963.

    Forms-Eigenschaftenfenster: Platzieren einer benutzerdefinierten Eigenschaft in eine SubCategory

    Hallo Community,

    Wie kann man eine selbst definierte Eigenschaft im Eigenschaftenfenster einer Form in eine vorhandene SubCategory, wie Appearance/​FlatAppearance (siehe Bild) plazieren?
    Ist das überhaupt möglich?

    VB.NET-Quellcode

    1. <Browsable(True)>
    2. <Category("Appearance")>
    3. <Description("Frame color of the button when it has the focus...")>
    4. Public Property FocusedBorderColor As Color = SystemColors.Highlight
    5. <[ReadOnly](True)>
    6. Public Overloads Property Flatstyle As FlatStyle = FlatStyle.Flat​



    lg
    VB1963
    Hm BorderSize ist zum Beispiel eine Eigenschaft von FlatButtonAppearance nicht von Button; vielleicht hat das damit zutun?
    Mich erinnert das ein wenig an WPF da macht man ja eine Darstellung aus einer beliebigen Klasse. Und da im Eigenschaftsfenster ja nicht der Button dargestellt ist, sondern dessen Eigenschaften wäre das so gemünzt BorderSize ja eine Darstellung der FlatAppearance und nicht des Buttons.
    @VB1963 Sieh Dir mal die Quellen im IlSpy an, ob da was verzeichnet ist.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Achso, also ist deine FocusBorderColor schon eine Eigenschaft von FlatButtonAppearance ich dachte, die wäre in deiner Button Klasse

    Mit einem Converter krieg ich einen Node im Eigenschaftsfenster erzeugt. Werden die Eigenschaft von Point ausgeschildert (X, Y), weil ich nen PointConverter geklaut habe. Aber ob man da noch Eigenschaften dazu wursteln kann... (Da Test kein Point ist meckert er da natürlich gewaltig, aber ich würde denken wenn man eine Klasse und einen Converter entsprechend bastelt, dann kann man so einen Node frei gestalten)

    VB.NET-Quellcode

    1. Public Class ButtonTest
    2. Inherits Button
    3. <Browsable(True)>
    4. Public Property Test As Test = New Test
    5. End Class
    6. Public Class TestX
    7. Inherits PointConverter
    8. End Class
    9. <TypeConverter(GetType(TestX))>
    10. Public Class Test
    11. End Class

    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „Haudruferzappeltnoch“ ()

    FocusedBorderColor kann nicht Teil von FlatAppearance werden, da FlatAppearance eine Klasse ist, deren Konstruktor versteckt ist, sodass man nicht davon erben kann, um die Klasse zu erweitern.
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    Also ich hätte es so nie hingekriegt, aber mit ein bisschen Glück konnte ich es GPT entlocken, das ergibt einen CustomButton mit einer Custom Eigenschaft, die wiederum customizable Eigenschaften in einer SubCategory beinhaltet:

    VB.NET-Quellcode

    1. Public Class CustomFlatAppearance
    2. <Browsable(True)>
    3. <Description("Die Farbe des Rahmens.")>
    4. Public Property BorderColor As Color
    5. <Browsable(True)>
    6. <Description("Die Größe des Rahmens.")>
    7. Public Property BorderSize As Integer
    8. End Class
    9. Public Class CustomFlatAppearanceConverter
    10. Inherits ExpandableObjectConverter
    11. Public Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
    12. If destinationType = GetType(CustomFlatAppearance) Then Return True
    13. Return MyBase.CanConvertTo(context, destinationType)
    14. End Function
    15. End Class
    16. Public Class CustomButton
    17. Inherits Button
    18. Private _customFlatAppearance As CustomFlatAppearance = New CustomFlatAppearance()
    19. <Browsable(True)>
    20. <Description("Die benutzerdefinierte Darstellung des Buttons.")>
    21. <TypeConverter(GetType(CustomFlatAppearanceConverter))>
    22. Public Property CustomFlatAppearance As CustomFlatAppearance
    23. Get
    24. Return _customFlatAppearance
    25. End Get
    26. Set(value As CustomFlatAppearance)
    27. _customFlatAppearance = value
    28. End Set
    29. End Property
    30. End Class
    FlatAppearance selbst anzwacken geht ja leider nicht wie @VaporiZed geschrieben hat, aber vielleicht kannst du die schlicht austauschen...
    Du könntest dir auch eine eigene FlatButtonAppearance Klasse, basierend auf der Originalen Klasse, erstellen.



    Beispiel

    C#-Quellcode

    1. using System;
    2. using System.ComponentModel;
    3. using System.Drawing;
    4. using System.Globalization;
    5. using System.Windows.Forms;
    6. namespace MyButton
    7. {
    8. internal sealed class ApplicableToButtonAttribute : Attribute
    9. {
    10. public ApplicableToButtonAttribute()
    11. {
    12. }
    13. }
    14. internal class FlatButtonAppearanceConverter : ExpandableObjectConverter
    15. {
    16. // Don't let the property grid display the full type name in the value cell
    17. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    18. {
    19. if (destinationType == typeof(string))
    20. {
    21. return "";
    22. }
    23. return base.ConvertTo(context, culture, value, destinationType);
    24. }
    25. // Don't let the property grid display the CheckedBackColor property for Button controls
    26. public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
    27. {
    28. if (context != null && context.Instance is Button)
    29. {
    30. Attribute[] attributes2 = new Attribute[attributes.Length + 1];
    31. attributes.CopyTo(attributes2, 0);
    32. attributes2[attributes.Length] = new ApplicableToButtonAttribute();
    33. attributes = attributes2;
    34. }
    35. return TypeDescriptor.GetProperties(value, attributes);
    36. }
    37. }
    38. [TypeConverter(typeof(FlatButtonAppearanceConverter))]
    39. public class FlatButtonAppearance
    40. {
    41. [Browsable(true), ApplicableToButton(), NotifyParentProperty(true), EditorBrowsable(EditorBrowsableState.Always), DefaultValue(1)]
    42. public int BorderSize
    43. {
    44. get => buttonOwner.FlatAppearance.BorderSize;
    45. set => buttonOwner.FlatAppearance.BorderSize = value;
    46. }
    47. [Browsable(true), ApplicableToButton(), NotifyParentProperty(true), EditorBrowsable(EditorBrowsableState.Always), DefaultValue(typeof(Color), "")]
    48. public Color BorderColor
    49. {
    50. get => buttonOwner.FlatAppearance.BorderColor;
    51. set => buttonOwner.FlatAppearance.BorderColor = value;
    52. }
    53. [Browsable(true), ApplicableToButton(), NotifyParentProperty(true),EditorBrowsable(EditorBrowsableState.Always),DefaultValue(typeof(Color), "")]
    54. public Color CheckedBackColor
    55. {
    56. get => buttonOwner.FlatAppearance.CheckedBackColor;
    57. set => buttonOwner.FlatAppearance.CheckedBackColor = value;
    58. }
    59. [Browsable(true), ApplicableToButton(), NotifyParentProperty(true),EditorBrowsable(EditorBrowsableState.Always),DefaultValue(typeof(Color), "")]
    60. public Color MouseDownBackColor
    61. {
    62. get => buttonOwner.FlatAppearance.MouseDownBackColor;
    63. set => buttonOwner.FlatAppearance.MouseDownBackColor = value;
    64. }
    65. [Browsable(true), ApplicableToButton(), NotifyParentProperty(true),EditorBrowsable(EditorBrowsableState.Always),DefaultValue(typeof(Color), "")]
    66. public Color MouseOverBackColor
    67. {
    68. get => buttonOwner.FlatAppearance.MouseOverBackColor;
    69. set => buttonOwner.FlatAppearance.MouseOverBackColor = value;
    70. }
    71. [Browsable(true), ApplicableToButton(), NotifyParentProperty(true), EditorBrowsable(EditorBrowsableState.Always), DefaultValue(typeof(Color), "")]
    72. public string MyFancyCustomFlatAppearanceProperty
    73. {
    74. get => "Change me if you can ;)";
    75. }
    76. private readonly Button buttonOwner;
    77. public FlatButtonAppearance(Button owner) => buttonOwner = owner ?? new Button();
    78. }
    79. }


    C#-Quellcode

    1. using System.ComponentModel;
    2. using System.Windows.Forms;
    3. namespace MyButton
    4. {
    5. public class MyButton : Button
    6. {
    7. [Browsable(true), Category("Appearance"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    8. public new FlatButtonAppearance FlatAppearance { get; private set; }
    9. public MyButton() : base() => FlatAppearance = new FlatButtonAppearance(this);
    10. }
    11. }

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Fakiz“ () aus folgendem Grund: Category-Attribute ergänzt