Thema: Prism TComboBox +.NET

Einzelnen Beitrag anzeigen

Robert_G
(Gast)

n/a Beiträge
 
#6

Re: TComboBox +.NET

  Alt 9. Dez 2004, 00:10
Ich wußte erst nicht, ob ich hier wirklich posten soll... Aber nun ist es zu spät.

Zitat von jbg:
Oder man nimmt VCL.NET
WinForms ist ja schon als deprecated auf den Markt gekommen.
Der war gut.

Zitat von skyware:
Finde es allerdings schon ziemlich umständlich zu handhaben...eieiei.
Solange du in VCL statt in FCL denkst ist natürlich alles kompliziert.

Es gibt in .Net so eine nette Sache, die nennt sich DataBinding.
Das heißt, du kannst alles was IList implementiert an jedes Control binden. (oder besser jedes Control an... )
Wie sieht das aus?

In dem Schnipsel wird eine Collection mit 4 Items gefüllt und anschließend an eine ComboBox und ein DataGrid gehängt:
Code:
   ItemWithIdCollection itemCollection = new ItemWithIdCollection();

   itemCollection.Add("eins", 1);
   itemCollection.Add("zwei", 2);
   itemCollection.Add("drei", 3);
   itemCollection.Add("vier", 4);
   itemCollection.DisplayFormat = "{1}: {0}";

   EineComboBox.DataSource = itemCollection;
   EinDataGrid.DataSource = itemCollection;
Es macht IMHO wenig Sinn Daten _IN_ ein Control zu stecken. Viel logischer ist es doch eine Collection daran zu binden, dadurch kannst du die Daten auch an ein anderes Control packen (siehe Anhang) und sie verschenden ohne einen Krampf dabei zu kriegen.
Dieses ständige herumgecaste aus den ComboBox Items wäre sicher ein Krampf.

Und hier ist die Itemklasse bzw, die CollectionBase-Ableitung (tippt sich schneller als es aussieht ):
BTW: CollectionBase ist der "faule" Weg einer typsicheren IList implementierung
Code:
using System.Collections;

namespace Samples.DataBinding.ComboBoxItems
{
   /// <summary>
   /// Ein bißchen Text mit einer numerischen ID.
   /// </summary>
   public class ItemWithId
   {
      string text;
      int id;
      internal ItemWithIdCollection collection;

      #region Properties

      public string Text
      {
         get { return text; }
         set { text = value; }
      }

      public int Id
      {
         get { return id; }
         set { id = value; }
      }

      #endregion
      /// <summary>
      /// Gibt die Daten/den Text Items aus.
      /// </summary>
      /// <remarks>
      /// Gehört das Item zu einer Collection, bei der <see cref="ItemWithIdCollection.DisplayFormat">DisplayFormat</see>
      /// besetzt wurde, wird dieses Format verwendet.
      /// </remarks>
      public override string ToString()
      {
         if (collection != null &&
            collection.DisplayFormat != string.Empty)
            return string.Format(collection.DisplayFormat,
                                 text,
                                 id);
         else
            return text;
      }

      #region construtors

      public ItemWithId(string text, int id)
      {
         this.text = text;
         this.id = id;
      }

      public ItemWithId()
      {
         text = string.Empty;
      }

      #endregion
   }


   public class ItemWithIdCollection : CollectionBase
   {
      string displayFormat;

      #region Properties
      /// <summary>
      /// Damit kann das Ausgabeformat betimmt werden.
      /// </summary>
      /// <remarks>
      /// Ist dieser Wert leer wird nur der Text dargestellt.
      /// Die Syntax entspricht <see cref="System.String.Format">String.Format</see>
      /// </remarks>
      public string DisplayFormat
      {
         get { return displayFormat; }
         set
         {
            if (value != null)
               displayFormat = value;
            else
               displayFormat = string.Empty;
         }
      }

      /// <summary>
      /// StiNo indexer ;-)
      /// </summary>
      public ItemWithId this[int index]
      {
         get { return base.List[index] as ItemWithId; }
         set { base.List[index] = value; }
      }

      #endregion

      public void Add(ItemWithId item)
      {
         item.collection = this;
         base.InnerList.Add(item);
      }

      public ItemWithId Add(string text, int id)
      {
         ItemWithId item = new ItemWithId(text, id);
         item.collection = this;
         base.InnerList.Add(item);
         return item;
      }

      public ItemWithIdCollection()
      {
         displayFormat = string.Empty;
      }
   }
}
Nachtrag:
Bevor jetzt jemand rumeckert: Ja es ist nicht Delphi und ja es ist C#. Warum? Ganz einfach, Delphi macht unter Win32 Sinn (fehlende Konkurrenz), unter .Net sehe ich keinen Sinn in der eingestaubten Delphi-Syntax.
Edit: gans böhse Schlechtschreibung.
Miniaturansicht angehängter Grafiken
samples.databinding.jpg  
  Mit Zitat antworten Zitat