c# - 根据子元素对堆栈面板进行排序?

标签 c# wpf linq

有没有一种方法可以根据其某些子元素对堆栈面板进行排序?

在后面的代码中,我向堆栈面板添加了一些通用的东西,例如组框和文本 block ,其中一个文本 block 信息是来 self 的网络服务的 DateTime,我尝试使用 linq 按降序排序,但输出仍然相同。

所以我想知道是否可以按堆栈面板子元素之一(即包含 DateTime 属性的 textblock1.Text)进行排序?

        XDocument xDoc = XDocument.Load(uriGroups);
        var sortedXdoc = xDoc.Descendants("Student")
                       .OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value));


        foreach (var node in xDoc.Descendants("Student"))
        {

            GroupBox groupbox = new GroupBox();
            groupbox.Header = String.Format(node.Element("StudentID").Value);
            groupbox.Width = 100;
            groupbox.Height = 100;
            groupbox.Margin = new Thickness(1);

            TextBlock textBlock = new TextBlock();
            textBlock.Text = String.Format(node.Element("FirstName").Value + " " + (node.Element("LastName").Value));
            textBlock.TextAlignment = TextAlignment.Center;

            TextBlock textBlock1 = new TextBlock();
            textBlock1.Text = (DateTime.Parse(node.Element("TimeAdded").Value)).ToString("d");
            String.Format("{0:d/M/yyyy}", DateTime.Parse(node.Element("TimeAdded").Value));
            textBlock1.TextAlignment = TextAlignment.Center;
            textBlock1.VerticalAlignment = VerticalAlignment.Bottom;

            StackPanel stackPanel = new StackPanel();
            stackPanel.Children.Add(groupbox);

            stackPanel.Children.Add(textBlock);
            stackPanel.Children.Add(textBlock1);
            stackPanel.Margin = new Thickness(5);
            stackPanel.MouseEnter += new MouseEventHandler(stackpanel_MouseEnter);
            stackPanel.MouseLeave += new MouseEventHandler(stackpanel_MouseLeave);
            MainArea1.Children.Add(stackPanel);
        }
    }

最佳答案

显示顺序完全由调用顺序定义

MainArea1.Children.Add(stackPanel);

所以,尝试类似的东西

 foreach (var node in xDoc.Descendants("Student").OrderBy(e => ...))
 {
    ....
 }

(你真的应该在这里使用模板)

关于c# - 根据子元素对堆栈面板进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10189318/

相关文章:

c# - WPF 将 ObservableCollection 绑定(bind)到 UserControl 的意外行为

c# - 使用 linq 展平数据

c# - 动态地在 Linq C# 中加入多表

c# - 使用 LINQ 在 C# 中的基于服务的数据库中搜索值是否存在

c# - 从 SQLConnection 对象或从 SqlCommand 对象调用 BeginTransaction 之间有区别吗?

c# - Xceed 属性网格 : Collection Editor Not Expanding In Proper Manner

c# - 在 json.Net 中创建命名子数组

wpf - 异步 MVVM 命令

c# - .NET SqlDataReader 对象是使用数据库游标,还是将整个结果集加载到 RAM 中?

c# - WPF MultiBinding VS 设计器异常