WPF Datagrid 对绑定到包含数字的 XML 的列进行排序

发布于 2024-09-08 16:46:44 字数 216 浏览 3 评论 0原文

我正在将 WPF Datagrid 绑定到 XML 源。我有一个代表对象 ID 的 DataGridTextColumn 列。当我对该列进行排序时,它会给出例如:1, 12, 13, 2, 3, 31, 4。

我显然想将其排序为 1, 2, 3, 4, 12, 13, 31

。有没有办法指定我要根据字符串的整数表示形式对列进行排序?

谢谢。

I am binding my WPF Datagrid to an XML source. I have a DataGridTextColumn column representing the Id of my object. When I sort on that column, it gives me for example: 1, 12, 13, 2, 3, 31, 4.

I obviously would like to sort it as 1, 2, 3, 4, 12, 13, 31.

Is there a way to specify that I want to sort the column based on the integer representation of the string?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妥活 2024-09-15 16:46:44

您需要自己进行排序。您可以使用 linq 轻松对 xml 进行排序。下面的示例按您在单击标题时所期望的方式对数字进行排序。我只实现了升序排序。

XAML:

<Window x:Class="DataGridDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    Height="300" Width="300">
    <StackPanel>
        <Controls:DataGrid 
            ItemsSource="{Binding Path=Trades}"
            Sorting="OnSorting">
            <Controls:DataGrid.Columns>
                <Controls:DataGridTextColumn Header="Side" Binding="{Binding Path=Attribute[Side].Value}" />
                <Controls:DataGridTextColumn Header="Price" Binding="{Binding Path=Attribute[Price].Value}" />
                <Controls:DataGridTextColumn Header="Volume" Binding="{Binding Path=Attribute[Volume].Value}" />
            </Controls:DataGrid.Columns>
        </Controls:DataGrid>
    </StackPanel>
</Window>

隐藏代码:

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Xml.Linq;
using Microsoft.Windows.Controls;

namespace DataGridDemo
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            DataContext = new VM();
        }

        private void OnSorting(object sender, DataGridSortingEventArgs e)
        {
            e.Handled = true;

            (DataContext as VM).SortCol = e.Column.Header as string;
        }
    }

    public class VM : INotifyPropertyChanged
    {
        public VM()
        {
            _data = 
                new XElement("Trades",
                             new XElement("Trade", new XAttribute("Side", "Buy"), new XAttribute("Price", "3.23"), new XAttribute("Volume", "100")),
                             new XElement("Trade", new XAttribute("Side", "Sell"), new XAttribute("Price", "13.12"), new XAttribute("Volume", "200")),
                             new XElement("Trade", new XAttribute("Side", "Buy"), new XAttribute("Price", "04.1"), new XAttribute("Volume", "15")),
                             new XElement("Trade", new XAttribute("Side", "Buy"), new XAttribute("Price", "30.78"), new XAttribute("Volume", "120")));

            SortCol = "Price";
        }

        private string _sortCol;
        public string SortCol
        {
            get { return _sortCol; }
            set
            {
                _sortCol = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(""));
                }
            }
        }

        public IEnumerable<XElement> Trades
        {
            get
            {
                if (SortCol == "Side")
                {
                    return from trade in _data.Elements("Trade")
                        orderby (string)trade.Attribute(SortCol)
                        select trade;
                }

                return
                    from trade in _data.Elements("Trade")
                    orderby (double)trade.Attribute(SortCol)
                    select trade;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private XElement _data;
    }
}

You will need to do the sorting yourself. You can use linq to easily sort xml. Below sample sorts numbers as you expect when clicking on headers. I only implemented ascending sort.

XAML:

<Window x:Class="DataGridDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    Height="300" Width="300">
    <StackPanel>
        <Controls:DataGrid 
            ItemsSource="{Binding Path=Trades}"
            Sorting="OnSorting">
            <Controls:DataGrid.Columns>
                <Controls:DataGridTextColumn Header="Side" Binding="{Binding Path=Attribute[Side].Value}" />
                <Controls:DataGridTextColumn Header="Price" Binding="{Binding Path=Attribute[Price].Value}" />
                <Controls:DataGridTextColumn Header="Volume" Binding="{Binding Path=Attribute[Volume].Value}" />
            </Controls:DataGrid.Columns>
        </Controls:DataGrid>
    </StackPanel>
</Window>

Code behind:

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Xml.Linq;
using Microsoft.Windows.Controls;

namespace DataGridDemo
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            DataContext = new VM();
        }

        private void OnSorting(object sender, DataGridSortingEventArgs e)
        {
            e.Handled = true;

            (DataContext as VM).SortCol = e.Column.Header as string;
        }
    }

    public class VM : INotifyPropertyChanged
    {
        public VM()
        {
            _data = 
                new XElement("Trades",
                             new XElement("Trade", new XAttribute("Side", "Buy"), new XAttribute("Price", "3.23"), new XAttribute("Volume", "100")),
                             new XElement("Trade", new XAttribute("Side", "Sell"), new XAttribute("Price", "13.12"), new XAttribute("Volume", "200")),
                             new XElement("Trade", new XAttribute("Side", "Buy"), new XAttribute("Price", "04.1"), new XAttribute("Volume", "15")),
                             new XElement("Trade", new XAttribute("Side", "Buy"), new XAttribute("Price", "30.78"), new XAttribute("Volume", "120")));

            SortCol = "Price";
        }

        private string _sortCol;
        public string SortCol
        {
            get { return _sortCol; }
            set
            {
                _sortCol = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(""));
                }
            }
        }

        public IEnumerable<XElement> Trades
        {
            get
            {
                if (SortCol == "Side")
                {
                    return from trade in _data.Elements("Trade")
                        orderby (string)trade.Attribute(SortCol)
                        select trade;
                }

                return
                    from trade in _data.Elements("Trade")
                    orderby (double)trade.Attribute(SortCol)
                    select trade;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private XElement _data;
    }
}
◇流星雨 2024-09-15 16:46:44

很好的答案和链接。

但是,如果您尝试对绑定到 DataTable 的 WPF DataGrid 进行排序,则只需确保 DataTable 已正确定义列类型。

例如:

DataTable dt = new DataTable("cats");
dt.Columns.Add("id", typeof(int)); // DataGrid will sort on this column correctly
dt.Add("name", typeof(String));
dt.Add("price", typeof(decimal));

Great answer and link.

However, if you are trying to sort a WPF DataGrid that is bound to a DataTable, you just need to make sure the DataTable has appropriately defined column types.

For example:

DataTable dt = new DataTable("cats");
dt.Columns.Add("id", typeof(int)); // DataGrid will sort on this column correctly
dt.Add("name", typeof(String));
dt.Add("price", typeof(decimal));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文