DataBinding



  • Hallo

    Vorne weg: Ich habe diese Frage bereist in einem anderem Forum gestellt, aber dort konnte man mir nicht weiterhelfen und so frage ich hier noch einmal. Ich möchte, dass dies nicht als unhöflich angesehen wird.

    Nun zu meinem Problem:
    Ich arbeite mit Visual Studio 2005 und habe einen Hauptdialog namens Form1. In der gleichnamigen Klasse habe ich einen Membervariable mit dem Namen back_cover_configs als Instanz der Klasse back_cover_config. Wenn der benutzer nun auf dem Hauptdialog eine Taste klickt, öffnet sich eine weiterer Dialog mit dem Namen back_config. Auf diesem Unterdialog befindet sich eine CheckBox namens show_left_text. Diese CheckBox würde ich gerne an eine variable der Instanz back_cover_configs binden, um so das updaten der CheckBox bzw. der Variable nicht von Hand machen zu müssen. Dazu habe ich im Designer bei den Properties der CheckBox unter DataBinding den CheckState gewählt und dann die Klasse back_cover_config angeklickt. Bei dieser Klasse habe ich dann das Propertie Show_left gewählt. Die Klasse back_cover_config sieht wie folgt aus:

    namespace new_Covermaker
    {
        public class back_cover_config : INotifyPropertyChanged
        {
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, e);
                }
            }
    
            private bool show_right = true;
            private bool show_left = true;
            private int right_alignment = 0;
            private int left_alignment = 1;
            private bool show_titel = true;
            private bool show_munbers = true;
            private bool show_artist = true;
            private bool show_album = true;
            private bool show_track = true;
    
            /// <summary>
            /// Gets or sets a value indicating whether this <see cref="back_cover_config"/> is show_right.
            /// </summary>
            /// <value><c>true</c> if show_right; otherwise, <c>false</c>.</value>
            public bool Show_right
            {
                get { return show_right; }
                set
                { 
                    show_right = value;
                    if (this.PropertyChanged != null)
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Show_right"));
                }
            }
    
            /// <summary>
            /// Gets or sets a value indicating whether this <see cref="back_cover_config"/> is show_left.
            /// </summary>
            /// <value><c>true</c> if show_left; otherwise, <c>false</c>.</value>
            public bool Show_left
            {
                get { return show_left; }
                set
                {
                    show_left = value;
                    if (this.PropertyChanged != null)
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Show_left"));
    
                }
            }
    
            /// <summary>
            /// Gets or sets the right_alignment.
            /// </summary>
            /// <value>The right_alignment.</value>
            public int Right_alignment
            {
                get { return right_alignment; }
                set { right_alignment = value; }
            }
    
            /// <summary>
            /// Gets or sets the left_alignment.
            /// </summary>
            /// <value>The left_alignment.</value>
            public int Left_alignment
            {
                get { return left_alignment; }
                set { left_alignment = value; }
            }
    
            /// <summary>
            /// Gets or sets a value indicating whether this <see cref="back_cover_config"/> is show_titel.
            /// </summary>
            /// <value><c>true</c> if show_titel; otherwise, <c>false</c>.</value>
            public bool Show_titel
            {
                get { return show_titel; }
                set { show_titel = value; }
            }
    
            /// <summary>
            /// Gets or sets a value indicating whether this <see cref="back_cover_config"/> is show_munbers.
            /// </summary>
            /// <value><c>true</c> if show_munbers; otherwise, <c>false</c>.</value>
            public bool Show_munbers
            {
                get { return show_munbers; }
                set { show_munbers = value; }
            }
    
            /// <summary>
            /// Gets or sets a value indicating whether this <see cref="back_cover_config"/> is show_artist.
            /// </summary>
            /// <value><c>true</c> if show_artist; otherwise, <c>false</c>.</value>
            public bool Show_artist
            {
                get { return show_artist; }
                set { show_artist = value; }
            }
    
            /// <summary>
            /// Gets or sets a value indicating whether this <see cref="back_cover_config"/> is show_album.
            /// </summary>
            /// <value><c>true</c> if show_album; otherwise, <c>false</c>.</value>
            public bool Show_album
            {
                get { return show_album; }
                set { show_album = value; }
            }
    
            /// <summary>
            /// Gets or sets a value indicating whether this <see cref="back_cover_config"/> is show_track.
            /// </summary>
            /// <value><c>true</c> if show_track; otherwise, <c>false</c>.</value>
            public bool Show_track
            {
                get { return show_track; }
                set { show_track = value; }
            }
    
        }
    }
    

    Mein erwartetes Verhalten war nun, dass die CheckBox angeklickt ist, wenn ich den Dialog öffne, da show_left ja true ist. Leider ist dies nicht der Fall.

    chrische



  • Hallo

    Ich schieb das mal hoch. Hat keiner eine Idee? Habe ich mich unverständlich ausgedrückt?

    chrische



  • Hast du denn schon mit dem Debugger überprüft, ob die get-Methode von Show_left aufgerufen wird?

    Was ich bei deinem Text noch nicht ganz verstehe: bindest du an ein Objekt oder an eine Klasse?



  • Hallo

    Das mit der Klasse bzw. dem Objekt versteh ich selber nicht. Ich wähle eignetlich nur die Klasse aus. Das kann so eignetlich nicht gehen. Ich weiß aber nicht wie ich die Instanz aus Form1 wählen kann.

    chrische



  • Hey, habe gerade deinen Thread bei myCSharp.de gelesen.
    Was passiert denn, wenn du beim Binding deine Variable angibst?

    this.show_left_text.DataBindings.Add(new System.Windows.Forms.Binding
     ("CheckState", /* -> */ this.back_cover_configs /* <- */, "Show_left", true));
    

    P.S: Ich bin auch dafür, daß zumindestens die Properties mit der .NET-Namenskonvention geschrieben werden (ist wirklich leserlicher!!!)

    public bool ShowLeft
    {
      ...
    }
    

    Und korrigiere auch mal den Namen 'Show_munbers' -)



  • Th schrieb:

    Hey, habe gerade deinen Thread bei myCSharp.de gelesen.
    Was passiert denn, wenn du beim Binding deine Variable angibst?

    this.show_left_text.DataBindings.Add(new System.Windows.Forms.Binding
     ("CheckState", /* -> */ this.back_cover_configs /* <- */, "Show_left", true));
    

    In dem von dir vorgestellten Code steht this.back_cover_configs. Das kann nicht klappen, da back_cover_configs member von form1 und show_left_text Member von back_configs ist.

    chrische



  • Dann hast du aber einen logischen Dreher in deinem Code.
    Irgendwie mußt du halt die Variable innerhalb deines Dialogs bekannt machen (denn wie soll sich der Dialog mit etwas verbinden, was er gar nicht kennt?).



  • Hallo

    Okay. Ich brauche die Informationen aber auch in Form1. Wäre das was für ein Singleton?

    chrische



  • Ob nun Singleton oder nicht ist nicht die Frage. Übergib einfach die Instanz an die Form1 weiter oder den this der anderen Form (nicht so schön).


Anmelden zum Antworten