Frage

    public class FontType
    {
        ...
        public String Name { get { return _name; } }
        public String DisplayName { get { return _displayName; } }
        public Font UseFont { get { return _font; } }
    }


bindFontTypes.DataSource = availableFonts;
comboFontType.DataSource = bindFontTypes;
comboFontType.ValueMember = "Key";
comboFontType.DisplayMember = ...???;

Hier bindFontTypes ist Binding. availableFonts ist ein Hashtable wo Keys Strings sind, und Werte sind Objekte von Fonttype. Für comboFontType.DisplayMember Ich möchte Objekte .DisplayName Eigenschaft verwenden. Wie lege ich das? Ist es möglich?

War es hilfreich?

Lösung

Es könnte funktionieren, wenn Sie

comboFontType.DisplayMember = "Value";  // FontType

und Überlastung ToString() für FontType.

Als Alternative für ToString () können Sie das Format-Ereignis des combobox verarbeiten kann.

Aber ich bin nicht einmal sicher, ob die Datenbindung auf diese Weise funktioniert.

Andere Tipps

Mit dem DisplayMember = "Value.DisplayName" Verwendung Ich bin geting die letzte zum Hashtable hinzugefügt ... ich arbeite immer sie alle ....

Das ist, was ich tat ... aber nur das letzte Element in der Hashtable bekommen zu binden ....

BindingSource src = new BindingSource();
            src.DataSource = new Hashtable 
            { 
            {
                "blah", 
                new FontType 
                { 
                    Name = "newFont", 
                    DisplayName = "new Font" 
                } 
                },
                { 
                    "another", 
                    new FontType 
                    {
                        Name = "anotherFont",
                        DisplayName = "another Font" 
                    } 
                    } 
            };
            comboBox1.DataSource = src;
            comboBox1.ValueMember = "Key";
            comboBox1.DisplayMember = "Value.DisplayName";
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top