2010-07-07 113 views
1

我將我的WPF Datagrid綁定到XML源。我有一個DataGridTextColumn列代表我的對象的Id。當我在那一欄上排序時,它給了我例如:1,12,13,2,3,31,4 4.WPF Datagrid在列上排序綁定到包含數字的XML

我顯然想分類爲1,2,3,4,12,13 ,31

有沒有一種方法來指定我想根據字符串的整數表示對列進行排序?

謝謝。

回答

1

您需要自己排序。你可以使用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; 
    } 
} 
+1

謝謝,我也發現這樣做的另一種方式:http://blogs.msdn.com/b/jgoldb/archive/2008/08/26/improving-microsoft-數據網格-CTP-分揀performance.aspx – joerage 2010-07-12 18:16:48

1

大答案和紐帶。

但是,如果您嘗試對與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));