如何将XML中的数据作为dataGrid中的数字排序 [英] How to sort data from XML as numeric in a dataGrid

查看:84
本文介绍了如何将XML中的数据作为dataGrid中的数字排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示在dataGrid中的XML文件中有数据。 (WPF,C#,VisualStudio 2013)列之一是数字数据。当我对列进行排序时,似乎将其转换为alpha而不是数字,尽管我尝试将该列指定为数字...

I have data in an XML file that I display in a dataGrid. (WPF, C#, VisualStudio 2013) One of the columns is numeric data. When I sort the column, it seems like it is translating it as alpha instead of numeric, though I have tried to specify that column as numeric...

dataGrid的XAML :

XAML for dataGrid:

<DataGrid 
    ItemsSource="{Binding Path=Elements[Record]}"
    AutoGenerateColumns="False" Height="Auto" 
    Name="dataGridRank" 
    VerticalAlignment="Top" HorizontalAlignment="Stretch">

    <DataGrid.Columns>
        <DataGridTextColumn 
           Header="Name" 
           Width="*"
           Binding="{Binding Element[Name].Value}"/>
        <DataGridTextColumn 
           Header="Rank" 
           Width="*"
           Binding="{Binding Element[Rank].Value, StringFormat={}{0:N}}"/>
    </DataGrid.Columns>

</DataGrid>

XMLfile'ranks.xml':

XMLfile 'ranks.xml':

<Ranks xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <Record>
     <Name> A </Name>
     <Rank dt:dt="number"> 85 </Rank>
  </Record>
  <Record> 
    <Name> C </Name>
    <Rank dt:dt="number"> 105 </Rank>
  </Record>
  <Record>
    <Name> B </Name>
    <Rank dt:dt="number"> 95</Rank>
  </Record>
</Ranks>

用于加载xml文件的C#代码:

C# code to load the xml file:

var xml = XDocument.Load("Resources/ranks.xml").Root;
dataGridRanks.DataContext = xml;

当我单击列标题以对排名进行排序时,我想要的是:

What I want when I click on the column header to sort Rank is:

A 85

B 95

C 105

or 

C 105

B 95

A 85

我能得到什么:

C 105

A 85

B 95

or 

B 95

A 85

C 105

无论是否使用Stringformat或dt:dt = number,我都尝试失败。我尝试过Stringformat = N以及StringFormat = {} {0:N}。

I have unsuccessfully tried both with and without the Stringformat or the dt:dt="number". I have tried Stringformat=N as well as StringFormat={}{0:N}.

我缺少什么?看来这应该很简单...

What am I missing? It seems like this should be simple...

推荐答案

由于元素值属性的类型为 string 。所以列类型是字符串,因此这里使用了字符串的默认比较器

Since Element value attribute is of type string. So column type is of string hence default comparer of string is getting used here.

StringFormat 在这里不起作用,因为它不会更改绑定属性的类型。

And StringFormat won't work here since it won't change the type of binded property. It's just used for formatting string on GUI.

我建议在运行时创建匿名类型,并将其与 Rank 属性的类型将为 int ,以便使用int的默认比较器。

I would suggest to create anonymous type at runtime and bind it with that where your Rank property will of be of type int so that default comparer of int gets used.

代码

var xml = XDocument.Load("database.xml").Root;
dataGridRank.ItemsSource = xml.Elements("Record")
                            .Select(e => new { Name = e.Element("Name").Value,
                             Rank = Convert.ToInt32(e.Element("Rank").Value) });

XAML:

    <DataGrid AutoGenerateColumns="False" Height="Auto" 
              Name="dataGridRank" 
              VerticalAlignment="Top" HorizontalAlignment="Stretch">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" 
                                Width="*"
                                Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Rank" 
                                Width="*"
                                Binding="{Binding Rank}"/>
        </DataGrid.Columns>

    </DataGrid>

这篇关于如何将XML中的数据作为dataGrid中的数字排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆