简体   繁体   中英

ListView with custom item layout in C#

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.

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 ;)");}       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM