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.

4

1 回答 1

-1

首先,ListView不会自己做任何事情。您需要设置ListView.ViewGridView.

我最近不得不解决动态列问题。我选择的解决方案是可绑定且兼容 MVVM 的,以防万一您想使用该模式(我曾经是)。我创建了一个行为(以避免扩展 GridView),它将在源结构更新时动态注入和删除列。此行为需要绑定到定义列的类的实例的依赖属性。列类应该允许您定义列,其中列是您在源数据上绑定的属性,以及一个键(表示单元格类型)。

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

当列结构发生变化时,行为会构建并将列注入(或删除)到附加的 GridView 中。行为基于行为上定义的一系列键/值对构建每一列。这是为了允许 XAML 指定要应用于新列的单元格模板,从而强制分离关注点。

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 ;)");}       
}
于 2013-07-30T09:19:12.173 回答