C# - Aktivieren/Deaktivieren von Feldern im "PropertyGrid" - Steuerelement
Ich entwickle eine Benutzer-Steuerelement-Bibliothek, in die ich zur Verfügung stellen müssen Programmierer, eine Eigenschaft raster zu passen Sie die Eigenschaften des Steuerelements.
Wenn der Programmierer verwendet System.Windows.Forms.PropertyGrid
(oder die Visual Studio-Designer)
einige der eigenschaftsfelder in die System.Windows.Forms.PropertyGrid
werden soll aktiviert/deaktiviert abhängig von einigen anderen Immobilie des gleichen users zu Steuern.
Wie es zu tun?
Beispiel-Szenario
Dies ist nicht die tatsächliche, sondern eine illustration.
Ex: UserControl1
hat zwei spezielle Eigenschaften:
MyProp_Caption: ein string
und
MyProp_Caption_Visible: bool
Nun, MyProp_Caption aktiviert werden soll, in das "PropertyGrid" - Steuerelement nur, wenn MyProp_Caption_Visible wahr ist.
Beispielcode für die UserControl1
public class UserControl1: UserControl <br/>
{
public UserControl1()
{
//skipping details
//label1 is a System.Windows.Forms.Label
InitializeComponent();
}
[Category("My Prop"),
Browsable(true),
Description("Get/Set Caption."),
DefaultValue(typeof(string), "[Set Caption here]"),
RefreshProperties(RefreshProperties.All),
ReadOnly(false)]
public string MyProp_Caption
{
get
{
return label1.Text;
}
set
{
label1.Text = value;
}
}
[Category("My Prop"),
Browsable(true),
Description("Show/Hide Caption."),
DefaultValue(true)]
public bool MyProp_Caption_Visible
{
get
{
return label1.Visible;
}
set
{
label1.Visible = value;
//added as solution:
//do additional stuff to enable/disable
//MyProp_Caption prop in the PropertyGrid depending on this value
PropertyDescriptor propDescr = TypeDescriptor.GetProperties(this.GetType())["MyProp_Caption"];
ReadOnlyAttribute attr = propDescr.Attributes[typeof(ReadOnlyAttribute)] as ReadOnlyAttribute;
if (attr != null)
{
System.Reflection.FieldInfo aField = attr.GetType().GetField("isReadOnly", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
aField.SetValue(attr, !label1.Visible);
}
}
}
}
Beispielcode für "PropertyGrid" - Steuerelement auf dieser UserControl1
- tstFrm ist ein einfaches Formular mit den folgenden zwei Daten-member
private System.Windows.Forms.PropertyGrid propertyGrid1; private UserControl1 userControl11;
Und wir können userControl1 durch propertyGrid1 wie folgt:
public partial class tstFrm : Form
{
public tstFrm()
{
//tstFrm embeds a PropertyGrid propertyGrid1
InitializeComponent();
propertyGrid1.SelectedObject = userControl11;
}
}
aktivieren/deaktivieren Sie das Feld MyProp_Caption im property-grid in Abhängigkeit vom Wert der MyProp_Caption_Visible?
danke! lösen können mit Ihrem Zeiger. Bearbeitet den code mit Lösung.
InformationsquelleAutor elimad | 2013-09-24
Du musst angemeldet sein, um einen Kommentar abzugeben.
(Beantwortet durch die OP in einem Frage Bearbeiten. Umgerechnet auf eine community-wiki-Antwort. Sehen Frage mit Nein Antworten, aber Problem gelöst, in den Kommentaren (oder erweitert im chat) )
Der OP schrieb:
InformationsquelleAutor