Felix Froschauer Felix Froschauer - 6 months ago 40
C# Question

dynamically generate a windows 10ish tile-menue in Windows Forms

Does anybody know a good way to generate tiles, sorted by groups dynamically?

Something like the tile view in windows 10(or in this case a devexpress example) Win 10 inspired Tile-Menue

The goal is to programmatically add groups or tiles by something like this: list.add("groupname","tilename"). So the groupname would be a primary key of group and a foreignkey of tile.

Answer Source

Here's a simple example I created to help you get started with the model part.

Let's say you have a Tile class that represents your tile. In that case, I put directly the group name as part of the tile, but you could also have a Group class and store the GroupId in the Tile.

public class Tile
    public string Title { get; set; }
    public string Group { get; set; }

Now, let's say you have created a few tiles like so :

 var tiles = new Tile[] {
     new Tile { Title = "Tile 1", Group = "Group 1" },
     new Tile { Title = "Tile 2", Group = "Group 1" },
     new Tile { Title = "Tile 3", Group = "Group 1" },
     new Tile { Title = "Tile 4", Group = "Group 2" },
     new Tile { Title = "Tile 5", Group = "Group 2" },
     new Tile { Title = "Tile 6", Group = "Group 2" }};

Now, you can group tiles by the Group parameter using a simple Linq query:

var groupedTiles = 
    from t in tiles
    group t by t.Group into g
    select new { Group = g.Key, Tiles = g.ToList() };

groupTiles will contain a list of "groups" that will contain each the list of attached tiles. You should be able to easily bind that to your view.

Here's a console output:

foreach (var group in groupedTiles)

    foreach (var tile in group.Tiles)
        Console.Out.WriteLine("\t" + tile.Title);

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download