【问题标题】:Sort elements in StackPanel alphabetically?按字母顺序对 StackPanel 中的元素进行排序?
【发布时间】:2015-01-01 18:06:08
【问题描述】:

我的网格中有一个 StackPanel,它显示了一个动态生成的按钮列表,我试图弄清楚如何让它们按字母升序显示。

XAML 代码

<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#代码

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
    }
}

【问题讨论】:

标签: c# wpf xaml


【解决方案1】:

我建议使用 MVVM 来完成这项任务。我正在发布一个以相当干净的方式工作的示例。

您的 XAML 应如下所示:

<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>

主视图模型。您也应该在这里对数据进行排序和过滤

 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);
        }
    }
}

按钮描述包含按钮的属性

    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()
    {

    }
}

如果您不熟悉 MVVM MVVM intro,我也建议您阅读以下内容

【讨论】:

    猜你喜欢
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2011-08-29
    • 2015-12-03
    • 1970-01-01
    相关资源
    最近更新 更多