首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

如何重载 DataGrid 点击列头的默认排序方法

2012-02-16 
怎么重载 DataGrid 点击列头的默认排序方法silverlight 的DataGrid通过设置DataGridTemplateColumn CanUse

怎么重载 DataGrid 点击列头的默认排序方法
silverlight 的DataGrid 通过设置DataGridTemplateColumn CanUserSort="True" 使其能点击表头进行默认排序方法的排序。


 但是我现在想重载这个方法,在点击表头后用我自定义的排序方法排序,求指教??

 

[解决办法]
你应该也在使用Telerik控件RadGridView。RadGridView可以实现自定义排序。实现方法应该和DataGrid差不多吧。RadGridView的例子:
XAML语句
<Examples:GridViewExample x:Class="Telerik.Windows.Examples.GridView.CustomSorting.Example"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Examples="clr-namespace:Telerik.Windows.Examples"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:telerikQuickStart="clr-namespace:Telerik.Windows.Controls.QuickStart;assembly=Telerik.Windows.Controls"
VerticalAlignment="Center" HorizontalAlignment="Center">

<telerikQuickStart:QuickStart.ExampleHeader>
<TextBlock Text="Client Accounts" Margin="8" />
</telerikQuickStart:QuickStart.ExampleHeader>

<Grid>
<Border telerikQuickStart:ThemeAwareBackgroundBehavior.IsEnabled="True" />
<telerik:RadGridView Name="RadGridView1" ItemsSource="{Binding Customers}" 
MinHeight="386" MaxHeight="500" Width="700"
CanUserFreezeColumns="False" RowIndicatorVisibility="Collapsed" 
AutoGenerateColumns="False" Sorting="RadGridView1_Sorting">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Customer ID" DataMemberBinding="{Binding CustomerID}" />
<telerik:GridViewDataColumn Header="Company Name" DataMemberBinding="{Binding CompanyName}" Width="*"/>
<telerik:GridViewDataColumn Header="Contact Name" DataMemberBinding="{Binding ContactName}" />
<telerik:GridViewDataColumn Header="City" DataMemberBinding="{Binding City}" />
<telerik:GridViewDataColumn Header="Country" DataMemberBinding="{Binding Country}" />
<telerik:GridViewDataColumn Header="Phone" DataMemberBinding="{Binding Phone}" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</Grid>
</Examples:GridViewExample>

C#语句
using System;
using System.Linq;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
using System.ComponentModel;
using Telerik.Windows.Controls.GridView;

namespace Telerik.Windows.Examples.GridView.CustomSorting
{
/// <summary>
/// Interaction logic for Example.xaml
/// </summary>
public partial class Example
{
public Example()
{
InitializeComponent();
}

private void RadGridView1_Sorting(object sender, GridViewSortingEventArgs e)
{
if (e.SortPropertyName == "CustomerID")
{
e.Cancel = true;

ColumnSortDescriptor descriptor = (from d in RadGridView1.SortDescriptors.OfType<ColumnSortDescriptor>()
where d.Column == RadGridView1.Columns["CompanyName"]
select d).FirstOrDefault();

if (descriptor == null)
{
RadGridView1.SortDescriptors.Add(new ColumnSortDescriptor()


{
Column = RadGridView1.Columns["CompanyName"],
SortDirection = ListSortDirection.Ascending
});

e.Column.SortingState = SortingState.Ascending;
}
else
{
descriptor.SortDirection = descriptor.SortDirection == ListSortDirection.Ascending ?
ListSortDirection.Descending : ListSortDirection.Ascending;

e.Column.SortingState = descriptor.SortDirection == ListSortDirection.Ascending ?
SortingState.Ascending : SortingState.Descending;
}
}
}
}
}

热点排行