0

I have a class called EventBox that extends TableLayoutPanel. It's a table with one single row and dynamically adjusting number of columns.

During its lifecycle, this EventBox adds/removes items from itself (buttons, combo boxes etc).

What I want is to create a ListView (or something similar) that would contain multiple EventBox objects and visually display them in a list.

I've created a class called TestEventList, but I do not know what to extend!

I've tried TableLayoutPanel (I believe it's overkill), ListBox (wrong!) and now ListView.

However, ListView's Items property has a method Add which only accepts ListViewItem objects as parameters.

How can I describe my EventBox as a ListViewItem?

Or better yet, what other choices do I have?

EDIT: I obviously want the list to be able to keep track of its items: add, remove at index etc.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
Bogdan Alexandru
  • 5,394
  • 6
  • 34
  • 54
  • can you just add rows to the `EventBox`? failing that, I think I would make a `UserControl` that contains a `TableLayoutPanel` with one column, and properties and method calls that will allow you to add new `EventBox`es which will each be placed in a new row on the single column `TableLayoutPanel`. – Dave Cousineau Jul 30 '13 at 09:01
  • You're looking for something like [this](http://stackoverflow.com/q/15532639/643085), which is built-into relevant current non-obsolete .Net Windows UI technologies. I strongly recommend using current non dinosaur stuff if you expect non-dinosaur-like results. winforms is a bad choice for all the reasons described in the link. Therefore you should use any of the XAML-based UI technologies. – Federico Berasategui Jul 30 '13 at 20:42

1 Answers1

-1

Firstly, ListView will not do anything on its own. You need to set ListView.View to an instance of GridView.

I recently had to solve the dynamic column problem. The solution I chose is bindable and MVVM compatible, just in case you want to use that pattern (i was). I created a behavior (to avoid extending GridView) that will dynamically inject and remove columns as a source structure updates. This behavior needs dependency property that you bind to a instance of a class that defines the columns. The column class should allow you to define columns where a column is the property you are binding to on the source data, and a key (to represent the cell type).

    public class ColumnDefinition
    {
        public string Key{ get; set}
        public string ContentBindingPath { get; set;}
    }

When the columns structure changes, the behavior builds and injects (or removes) columns into the attached GridView. The behavior builds each column based upon a series of key/value pairs defined on the behavior. This is to allow the XAML to specify the cell template to apply to the new columns, enforcing seperation of concerns.

public class CellTemplateDefinition
{
    public string Key { get; set; }
    public DataTemplate ColumnTemplate { get; set;}
}

public class DynamicColumnBehavior: Behavior<GridView>
{
     public IEnumerable<ColumnDefinition> Columns
     {
         get { return (IEnumerable<ColumnDefinition>)GetValue(ColumnsProperty); }
         set { SetValue(ColumnsProperty, value); }
     }

     // Using a DependencyProperty as the backing store for Columns.  This enables animation, styling, binding, etc...
     public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(IEnumerable<ColumnDefinition>), typeof(DynamicColumnBehavior), new UIPropertyMetadata(null));

     public static void OnColumnsChanged(DependencyObject sender, DependencyPropertyChangedEventArgsargs)
     {
         DynamicColumnBehavior behavior = sender as DynamicColumnBehavior;
         if(behavior != null) behavior.UpdateColumns();
     }

     public IEnumerable<CellTemplateDefinition> Cells { get; set; } 

     private void UpdateColumns(){ throw new NotImplementedException("I left this bit for you to do ;)");}       
}
Gusdor
  • 14,001
  • 2
  • 52
  • 64
  • Sorry but I don't see how this helps. My only problem is arranging my `EventBox`s objects in a vertical list, i.e. creating a listview with custom layout for the items (in this case, a TableLayoutPanel). – Bogdan Alexandru Jul 30 '13 at 09:24
  • Yeh, I totally didnt read that you are using Winforms. This is useless to you. For future, if you want a dynamic UI with nested controls, consider WPF (if possible). It is leaps ahead of winforms. – Gusdor Jul 30 '13 at 09:29