# Data Driven Approach

## Content

<span id="4b5d77b8-8118-41e7-bec3-8f3b59a9a2d6" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="4b5d77b8-8118-41e7-bec3-8f3b59a9a2d6" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">The data-driven approach generates tools dynamically from a data source using </span>**<span id="4b5d77b8-8118-41e7-bec3-8f3b59a9a2d6" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="4b5d77b8-8118-41e7-bec3-8f3b59a9a2d6" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">ItemsSource</span>**<span id="4b5d77b8-8118-41e7-bec3-8f3b59a9a2d6" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="4b5d77b8-8118-41e7-bec3-8f3b59a9a2d6" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation"> and an </span>**<span id="4b5d77b8-8118-41e7-bec3-8f3b59a9a2d6" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="4b5d77b8-8118-41e7-bec3-8f3b59a9a2d6" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">ItemTemplate</span>**<span id="4b5d77b8-8118-41e7-bec3-8f3b59a9a2d6" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="4b5d77b8-8118-41e7-bec3-8f3b59a9a2d6" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">. Use this method when creating the toolbar programmatically, such as when tools are user-configurable, loaded from a database, or generated based on application state. This approach enables flexible, scalable, and highly customizable tool creation.</span>
![](https://cdn.mescius.io/document-site-files/images/f5b600ba-f1a7-4f89-a20c-aa6c0c35880d/datadriven.15f778.png?width=600)

### **Create Toolstrip from Data Source**

1. <span id="80e71b4c-7f30-42c4-b8a2-7a81dda290d7" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="80e71b4c-7f30-42c4-b8a2-7a81dda290d7" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">Create a data model:</span>

```auto
 public enum ToolType
    {
        C1DropDownTool,   
        C1ToolSeparator,
        C1ButtonTool,
        C1SelectTool,
        C1ToggleButtonTool,
        C1ContentControlTool,
        C1MenuTool
    }

    public class ToolItem
    {
        public ToolType Type { get; set; }
        public string Label { get; set; }
        public string Description { get; set; }
        public RenderFragment Content { get; set; }  // For DropDownTool content
    }
```

2. <span id="ea5868ab-3e70-444a-b183-9fc642371633" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="ea5868ab-3e70-444a-b183-9fc642371633" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">Create a tool list:</span>

```auto
private List<ToolItem> ToolItems = new()
    {
        // create new dropdown
        new ToolItem
        {
            Type = ToolType.C1DropDownTool,
            Label = "options",
            Description = "(C1DropDownTool)",
        },

        // create new tool seperator "|"
        new() { Type = ToolType.C1ToolSeparator},
        
        // Create new C1Button Tool
        new() { Type = ToolType.C1ButtonTool, Label = "Save" },
        new() { Type = ToolType.C1ButtonTool, Label = "Edit" },
        new() { Type = ToolType.C1ButtonTool, Label = "View" }
    };
```

3. <span id="d18c7cb9-8fb1-43bb-a968-c740da0d3555" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="d18c7cb9-8fb1-43bb-a968-c740da0d3555" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">Bind ToolStrip and render each tool using ItemTemplate:</span>

```auto
<C1ToolStrip @ref="toolStrip" GroupCornerRadius="16"
                 Style="@($"background: linear-gradient(90deg, #f5f7fa 0%, #c3cfe2 100%); border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.08); padding: 12px; border: 1px solid #e0e0e0;")"
                 OverflowDropDownStyle="@("background: green !important; padding: 12px !important; border: 3px solid red !important;")"
                 ToolStyle="@("border: thin solid; margin: 3px;")">
        @foreach (var item in ToolItems)
        {
            // Render method for rendering different controls
            @RenderTool(item)
        }

    </C1ToolStrip>
```

4. <span id="6c05baac-822a-4ced-a089-67d50c352a26" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="6c05baac-822a-4ced-a089-67d50c352a26" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">Render each tool based on its type:</span>

```csharp
 private RenderFragment RenderTool(ToolItem item) => builder =>
    {
        switch (item.Type)
        {
            case ToolType.C1ToolSeparator:
                builder.OpenComponent<C1ToolSeparator>(0);
                builder.CloseComponent();
                break;

            case ToolType.C1ButtonTool:
                builder.OpenComponent<C1ButtonTool>(0);
                builder.AddAttribute(1, "Label", item.Label);
                builder.CloseComponent();
                break;

            case ToolType.C1DropDownTool:
                builder.OpenComponent<C1DropDownTool>(0);
                builder.AddAttribute(1, "Label", item.Label);
                builder.AddAttribute(2, "Description", item.Description);

                if (item.Content != null)
                {
                    builder.AddAttribute(4, "ChildContent", item.Content);
                }
                builder.CloseComponent();
                break;
        }
```

### **<span id="d7bfdf24-ca4e-429a-8929-e21ee95bdf9f" data-mark-type="annotation" data-mark-annotation-type="inlineComment" data-id="d7bfdf24-ca4e-429a-8929-e21ee95bdf9f" data-prosemirror-content-type="mark" data-prosemirror-mark-name="annotation">Data-Driven Approach Sample</span>**

```auto
<div>
    <h2>
        Data Driven
    </h2>
    <C1ToolStrip @ref="toolStrip" GroupCornerRadius="16"
                 Style="@($"background: linear-gradient(90deg, #f5f7fa 0%, #c3cfe2 100%); border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.08); padding: 12px; border: 1px solid #e0e0e0; {(((Orientation)orientation)==Orientation.Vertical?"height: 300px;":"")};")"
                 Orientation="@((Orientation)orientation)"
                 IsToolLabelVisible="@isLabelVisible.Value"
                 ToolOrientation="@((Orientation)toolOrientation)"
                 Overflow="@((ToolStripOverflow)overflowMode)"
                 ToolStyle="@("border: thin solid; margin: 3px;")">
        @foreach (var item in ToolItems)
        {
            @RenderTool(item)
        }

    </C1ToolStrip>
</div>

<p>In the sample tools are created as components using the <code>C1ToolStrip.ChildContent.</p>

class="badge text-bg-secondary">C1ToolStrip.ChildContent</span>
class="badge text-bg-secondary">RendererInfo: @RendererInfo.Name </span>


@code {

    public enum ToolType
    {
        C1DropDownTool,   
        C1ToolSeparator,
        C1ButtonTool,
        C1SelectTool,
        C1ToggleButtonTool,
        C1ContentControlTool,
        C1MenuTool
    }

    public class ToolItem
    {
        public ToolType Type { get; set; }
        public string Label { get; set; }
        public string Description { get; set; }
        public RenderFragment Content { get; set; }  // For DropDownTool content
    }

    private List<ToolItem> ToolItems = new()
    {
        new ToolItem
        {
            Type = ToolType.C1DropDownTool,
            Label = "options",
            Description = "(C1DropDownTool)",
        },
        new() { Type = ToolType.C1ToolSeparator},
        new() { Type = ToolType.C1ButtonTool, Label = "Save" },
        new() { Type = ToolType.C1ButtonTool, Label = "Edit" },
        new() { Type = ToolType.C1ButtonTool, Label = "View" }
    };

    private RenderFragment RenderTool(ToolItem item) => builder =>
    {
        switch (item.Type)
        {
            case ToolType.C1ToolSeparator:
                builder.OpenComponent<C1ToolSeparator>(0);
                builder.CloseComponent();
                break;

            case ToolType.C1ButtonTool:
                builder.OpenComponent<C1ButtonTool>(0);
                builder.AddAttribute(1, "Label", item.Label);
                builder.CloseComponent();
                break;

            case ToolType.C1DropDownTool:
                builder.OpenComponent<C1DropDownTool>(0);
                builder.AddAttribute(1, "Label", item.Label);
                builder.AddAttribute(2, "Description", item.Description);

                if (item.Content != null)
                {
                    builder.AddAttribute(4, "ChildContent", item.Content);
                }
                builder.CloseComponent();
                break;
        }
    };
}
```