Examples

Sample codes

Running codes with sources available here AutoListSamples.zip

Hello AutoList

0. Include AutoList.Control.dll to your project.

1. Create your ViewModel. Please note, that you must decorate the ItemsSource property with AutoListItemsSource attribute.

public class MainWindowViewModel
    {
       [AutoListItemsSource("Main")]
       public ObservableCollection<ListItem> Items { get; private set; }

       public MainWindowViewModel()
       {
          this.Items = new ObservableCollection<ListItem>();

          this.Items.Add(new ListItem() { Name = "Bob", Age = 22 });
          this.Items.Add(new ListItem() { Name = "Mary", Age = 19 });
       }
    }


2. Create a meta item class to show. Also: you must append AutoListHeader to every property you want to show in the list. This attribute allows you to append multiple times with different configurations if you want to show this item in multiple lists.

public class ListItem
   {
      [AutoListHeader("Name", 10, "Main")]
      public string Name { get; set; }
      [AutoListHeader("Age", 20, "Main")]
      public int Age { get; set; }
   }


3. Then your View, include AutoList.Control namespace and add an AutoList.

<Window x:Class="AutoListDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:control="clr-namespace:AutoList.Control;assembly=AutoList.Control"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <control:AutoList  ItemsSource="{Binding Items}"/>
    </Grid>
</Window>


How to modify AutoList in code behind: OnGenerated property

1. Create an Action<AutoListColumnConfiguration> property and initialize it
  public Action<AutoListColumnConfiguration> OnGeneratedAction { get; private set; }

  public SampleClass()
  {
    this.OnGeneratedAction = new Action<AutoListColumnConfiguration>(OnGenerated);
  }


2. Bind this property to the OnGenerated property in XAML.
  OnGenerated="{Binding OnGeneratedAction}"


3. Create the OnGenerated method. After AutoList finished to generated columns based on Attributes you can modify it through AuotListColumnConfiguration. You don't have to do it right in this method, you can store it to a local variable and modify it anytime.
  private void OnGenerated(AutoListColumnConfiguration e)
  {
    // custom code here, either store and modify it later or modify it here
  }


OnItemDoubleClick binding

1. Create a property to bind. It must be Action<object>, won't work with a specific type
  public Action<object> OnItemDoubleClicked { get; private set; }


2. Initialize and implement the action
  this.OnItemDoubleClicked = new Action<object>(OnListItemDoubleClicked);

  private void OnListItemDoubleClicked(object item)
  {
      // cast item to your type and execute custom code
  }


3. Bind it to the AutoList
  OnItemDoubleClick="{Binding OnItemDoubleClicked}"


Grouping

Simple property

1. Crearte a property to bind, like
  public Func<object, object> GroupByPredicate { get; set; }


2. Initialize and define
  this.GroupByPredicate = item => (item as ListItem).Name;


3. Bind
  GroupByExpression="{Binding GroupByPredicate}"

Using property selector

1. Create a property and a property selector list
  private Func<object, object> _GroupByPredicate = null;
  public Func<object, object> GroupByPredicate
  {
    get
    {
       return this._GroupByPredicate;
     }
     set
     {
       if (this._GroupByPredicate != value)
       {
           this._GroupByPredicate = value;
           this.RaisePropertyChanged("GroupByPredicate");
       }
    }
  }      

  public Dictionary<string, Func<object, object>> GroupByProperties { get; private set; } 


2. Here comes the tricky part, include AutoList.Base.dll in your project and add AutoList.Base.Extension to the using list. Fill the GroupByProperties property using the extension method. It helps you to discover a type and returns property selector func with name.
  this.GroupByProperties = TypeExtensions.GetPropertySelectors<ListItem>(p=>
       p.GetCustomAttributes(typeof(AutoListHeaderAttribute), false).Any());         


3. Bind this list to e.g. a combobox
<ComboBox 
       ItemsSource="{Binding GroupByProperties}" 
       DisplayMemberPath="Key" SelectedValuePath="Value" 
       SelectedValue="{Binding GroupByPredicate, Mode=TwoWay}" />                


4. Bind GroupByProperty to the AutoList
  GroupByExpression="{Binding GroupByPredicate}"


5. Now, you are able to change grouping on the fly in runtime.

Last edited Sep 7, 2013 at 10:51 AM by andrassebo, version 20

Comments

No comments yet.