This project is read-only.
Generators (Prototypes)

Resides in the namespace FasterWPF.Generators in the Generators static class.

Generators are static methods available to add multiple Composites OR multiple Children at one time.

This is achieved by leveraging a Prototype object that each generated object will be cloned from. Moreover, an Action Constructor delegate may be passed-in to be applied to the generated objects as they are created. This is necessary to allow Initialization of any generated IParents as well as the opportunity to add Settings. The Action Constructor also provides the opportunity to vary the generated objects based on an index or, in some cases, based on a row/column. For instance, if you are generating five rectangles, you could make the first four green and the last one orange.

There are four steps to implementing generators:

1. Create a Prototype
Button prototypButton = new Button();

2. Create a Constructor Action To Be Run Against Each Generated (Cloned) Element.
NOTE: This is IMPORTANT for Initialization and Settings! However, in this example I'm just setting a few properties.
Action<Button> myConstructor2 = prot => { prot.Visibility = Visibility.Visible;  prot.MinHeight = 100D; prot.MinWidth = 100D; prot.BorderBrush = Brushes.Green; prot.BorderThickness = new Thickness(2); prot.Background = Brushes.AliceBlue; prot.HorizontalAlignment = HorizontalAlignment.Left; };

3. Generate the Elements Based On the Prototype/Constructor and Add to Composites According to Range Provided Optionally, you may define settings for a Grid Composite, if you are using one.
List<string> keys2 = Generators.AddComposites<Button>(myGrid, 0, 1, 0, 2, 1, 2, prototypButton, myConstructor2, compositeGridSettings);

4. Manipulate Each Individual Element After the Fact, If Desired
Button b1 = myGrid.GetChildViaLookupDictionary<Button, Grid>(keys2[0].ToString(), 0, 1);
b1.Content = "1";
Button b2 = myGrid.GetChildViaLookupDictionary<Button, Grid>(keys2[1].ToString(), 0, 1);
b2.Content = "2";

The example below showcases the Generator that produces Labels and TextBoxes within a GroupBox:


The code:
#region Add Labels and Fields - Generator Test

string testGen = Guid.NewGuid().ToString();
GroupBox grbx = new GroupBox();
grbx.Margin = new Thickness(4);
grbx.Padding = new Thickness(4);

ContemporaryStyle.CreateGroupBoxHeader(grbx, "Test Generator", Brushes.Navy, Brushes.White, "Segoe UI", 12D, FontWeights.SemiBold, FontStyles.Normal, Brushes.Navy, new Thickness(2));
grbx.Initialize(300D, 300D, ContainerType.ContentControlPanel);

myGrid.BeginComposite<Grid>(testGen).AddExisting<GroupBox, Grid>(0, 1, grbx).SubscribeMouseSingleClickOnChild<GroupBox, Grid>(0, 1, (o, ev) => { MessageBox.Show("Hey! You clicked on the GroupBox."); }).EndComposite<Grid, GridArgs>(new GridArgs(1, 2));

           //Prototype Label
           Label lblPrototype = new Label();
           lblPrototype.Width = 115D;
           lblPrototype.Height = 39D;
           lblPrototype.Margin = new Thickness(17, 5, 7, 5);
           lblPrototype.Background = Brushes.CornflowerBlue; 
           lblPrototype.Foreground = Brushes.White;
           Action<int, Label> actLbl = (j, lbl) => { 
               //vary the one at index 4
               if (j == 4) { lbl.Background = Brushes.Silver; } 

           //Prototype TextBox
           TextBox txtBoxPrototype = new TextBox();
           txtBoxPrototype.Width = 150D;
           txtBoxPrototype.Height = 39D;
           txtBoxPrototype.Margin = new Thickness(7, 5, 7, 5);
           txtBoxPrototype.Background = Brushes.AliceBlue;
           Action<int, TextBox> actTxtBox = (z, txtBx) => { //no need to set anything here at this time. No initialization or settings needed. Empty constructor.
           List<string> labelCaptions = new List<string>();
           labelCaptions.Add("Label 1");
           labelCaptions.Add("Label 2");
           labelCaptions.Add("Long Label 3");
           labelCaptions.Add("Label 4");
           labelCaptions.Add("Label 5");

List<string> grbxKeys = Generators.AddLabelsAndFields(grbx, labelCaptions, 0, 0, 4, 41D, lblPrototype, actLbl, txtBoxPrototype, actTxtBox);

Generators may produce multiple Composites OR multiple Children within a single Composite. This example showcases merely adding a single Horizontal Panel Composite into a cell in a Grid and generating 5 labels in a row within that Composite.


The code:
Label prototypeL = new Label();
         prototypeL.Width = 15D;
         prototypeL.Height = 15D;
Action<Label> constructorL = ( lb) => 
           lb.Background = Brushes.SeaShell;
           lb.BorderBrush = Brushes.Black;
           lb.BorderThickness = new Thickness(1);
           //potentially vary the first one: if (t == 1) { lb.Background = Brushes.Coral; }

string labelKey = Generators.AddChildrenInRange(stackpanelAtCell1_1, 0, 0, 4, 4, 1,2, prototypeL, constructorL);

Last edited Mar 15, 2014 at 5:55 PM by stagathome0069, version 17


No comments yet.