Marcus Ribeiro
Marcus Ribeiro

Reputation: 1

Sort elements in StackPanel alphabetically?

I have a StackPanel in my grid and it displays a dynamically generated list of buttons, I'm trying to figure out how to get them displayed ascending alphabeticaly.

XAML Code

<StackPanel Grid.Column="0" Name="AreaStackPanel" Orientation="Vertical" Background="Khaki">
</StackPanel>

<StackPanel  Grid.Column="1"  Orientation="Horizontal" Background="Beige">
    <GroupBox Name="StatusGroupBox" Header="Work Items" Width="234">
        <StackPanel Name="StatusStackPanel"></StackPanel>
    </GroupBox>
</StackPanel>

C# Code

private void LoadExistingAreas()
{
    List<string> collections = Reporter.GetCollections();

    string unique = "";

    foreach (string collection in collections)
    {
        string areaName = Path.GetFileName(collection);

        if (unique.Contains(areaName)) continue;
        unique += areaName;

        Button areaButton = new Button();
        areaButton.Click += areaButton_Click;
        areaButton.Margin = new Thickness(2);
        areaButton.Content = areaName;
        AreaStackPanel.Children.Add(areaButton);
        Area
    }
}

Upvotes: 0

Views: 1189

Answers (1)

joshwl2003
joshwl2003

Reputation: 463

I would recommend using MVVM to accomplish this task. I am posting an example of what would work in a fairly clean fashion.

Your XAML should look as follows:

<Window x:Class="ItemTemplateDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ItemTemplateDemo"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>
<Grid>
    <ItemsControl ItemsSource="{Binding ButtonDescriptions}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel></StackPanel>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Margin="2" Content="{Binding Name}" Command="{Binding OnClickCommand}"></Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

The main view model. You should sort and filter your data in here as well

 public class MainViewModel
{
    public ObservableCollection<ButtonDescription> ButtonDescriptions { get; private set; }

    public MainViewModel()
    {
        ButtonDescriptions = new ObservableCollection<ButtonDescription>();

        for (int i = 0; i < 10; i++)
        {
            var bd = new ButtonDescription() { Name = "Button " + i };

            ButtonDescriptions.Add(bd);
        }
    }
}

The button description holds the attributes for the button

    public class ButtonDescription
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private ICommand onClickCommand;
    public ICommand OnClickCommand
    {
        get { return onClickCommand; }
        set { onClickCommand = value; }
    }

    public ButtonDescription()
    {

    }
}

I would also recommend reading the following if you are not familiar with MVVM MVVM intro

Upvotes: 2

Related Questions