Skip to content

AutoCollection

ElbyFross edited this page Dec 14, 2019 · 4 revisions

Remarks

  • Derived from the CollectionControl.
  • Compatible with the autolayout conception via the UIDescriptor.
  • Implements the System.Collections.IList interface.
  • Implements the WpfHandler.UI.AutoLayout.IGUIField interface.
  • Handle the IGUIField.ValueChanged event to respond when the user changes a AutoCollection.
  • In case if the source is IsFixedSize of IsReadOnly then blocks the add and remove buttons, and disabling reorder option.
  • In case if the source has generic types, then the first one will be used as the default type for the list items during OnAdd callback handling. You can override that handler by subscribing the custom handler on the AutoCollection.OnAddEvent.

Examples

XAML only

In that example we'll define the static array resource and apply it as the source of the AutoCollection.

XAML

Definig the array like a static resource.

<Window.Resources>
   <x:Array Type="{x:Type sys:String}" x:Key="stringStaticArray">
       <sys:String>A</sys:String>
       <sys:String>B</sys:String>
       <sys:String>C</sys:String>
   </x:Array>
</Window.Resources>

Applying the stringStaticArray as a value of the AutoCollection control.

<wpfh:AutoCollection Value="{StaticResource ResourceKey=stringStaticArray}"/>

XAML + Code behind

In that example we'll define the source of the AutoCollection defined at the XAML from the code behind.

XAML

<wpfh:AutoCollection x:Name="collection" MaxHeight="200"/>

C#

// Binding the int collection to the second AutoCollection control.
var intCollection = new List<int>(new int[] { 0, 20, 40, 60 });
collection.Value = intCollection;

UIDescriptor

In that example we'll consider the few case of the defining AutoCollection from the class derived from UIDescriptor.

Look also: Autolayout getting started

C#

// Declaring the int array member.

// The collection binded to that source will not has control buttons.
// Also you would unable to reorder the elements.
// That's caused by the `IsFixedSize` state of the `Array`.

// By default instiniated elements will be represented 
// by `FlatTextBox` control with the `ValueMode` equal to `Mode.Int`.
public int[] intArray = new int[]{0, 1, 2, 3, 4, 5};
// The `List` member fully compatible with 
// all features of the `AutoCollection` control.

// `Add Event` not overridden so the collection will manage 
// the string type (the first generic type) 
// like the default for all new items added via UI.
public List<string> stringList = new List<string>();

// The empty costructor of the descriptor.
public ACExample()
{
    // Adding elemetns to the string list.
    stringList.Add("string 1");
    stringList.Add("string 2");
    stringList.Add("string 3");
}

Now let's add a collection with a flexible elements type. Also we'll costomise the element properties and override the default OnAddClick handler by new one that add the new int field.

flexibleCollection has defined the object generic type. AutoCollection unable to instantiate the IGUIField for the object member by default. That could be fixed by registing the object competible IGUIField into the autolayout handler. More about...

C#

// Enum that will be used like a source for 
// the toggle control.
public enum Mode
{
    Option1,
    Option2,
    Option3
}

// Redefining some collection options.
[AutoCollectionProperties(
    // Disabling splitters.
    SplitersDraw = false,

    // Disabling items drag.
    DragAllowed = false,

    // Disabling remove button.
    RemoveButtonVisibile = false)]
public List<object> flexibleCollection = new List<object>();

public ACExample()
{
   ...

   // Adding the enum field.
   flexibleCollection.Add(Mode.Option1);

   // Adding the string field.
   flexibleCollection.Add("example text field");

   // Adding the double field.
   flexibleCollection.Add(2.0d); 



   // Subsribing to the descriptor loading event.
   Loaded += ACExample_Loaded;
}

// Occurs when descripto UI instiniated and ready to use.
private void ACExample_Loaded(UIDescriptor obj)
{
    // Looking for the UI field binded to the `flexibleCollection` member.
    var field = GetFieldByMember("flexibleCollection");
    var collection = field as AutoCollection;

    // Subscribing on the `Add item` event.
    collection.OnAddClick += delegate (object sender)
    {
         // Adding the int field.
         collection.Add(0);
    };
}

#Links

Projects

Relative pages