C# Silverlight 4-相关表datagrid显示排序

C# Silverlight 4-相关表datagrid显示排序,c#,silverlight-4.0,C#,Silverlight 4.0,我正在使用中的示例代码将相关数据显示到数据网格中。在我的数据中,我需要按年份对表进行排序(我的相关表中的一个名为“\u year”的属性)。我不知道如何在xaml或c#中实现这一点。以下是我目前的代码: xaml: C#: private void SelectedRoadsTreeView\u SelectedItemChanged(对象发送方,RoutedPropertyChangedEventArgs e) { 如果(e.OldValue!=null) { 图形g=e。作为图形的旧值;

我正在使用中的示例代码将相关数据显示到数据网格中。在我的数据中,我需要按年份对表进行排序(我的相关表中的一个名为“\u year”的属性)。我不知道如何在xaml或c#中实现这一点。以下是我目前的代码:

xaml:


C#:

private void SelectedRoadsTreeView\u SelectedItemChanged(对象发送方,RoutedPropertyChangedEventArgs e)
{
如果(e.OldValue!=null)
{
图形g=e。作为图形的旧值;
g、 取消选择();
g、 SetZIndex(0);
}
如果(如NewValue!=null)
{
图形g=e,新值为图形;
g、 选择();
g、 SetZIndex(1);
//关系查询
RelationshipParameter relationshipParameters=新的RelationshipParameter()
{
objectId=new int[]{Convert.ToInt32(g.Attributes[SelectedRoadsTreeView.Tag as string]),
外场=新串[]{“loc_id,count_id,_year,adt”},
RelationshipId=0,
OutSpatialReference=Map.SpatialReference
};
queryTask.ExecuteRelationshipQueryAsync(关系参数);
MyChart2.Title=g.Attributes[“TrafficCounts.COUNTUSER.%segment\u master.\u street”];
}
}
void QueryTask\u ExecuterRelationshipQueryCompleted(对象发送方,RelationshipEventArgs e)
{
关系结果pr=e.结果;
if(pr.RelatedRecordsGroup.Count==0)
{
RelatedRowsDataGrid.ItemsSource=null;
}
其他的
{
foreach(pr.RelatedRecordsGroup中的变量对)
{
RelatedRowsDataGrid.ItemsSource=pair.Value;
//?.SortDescriptions.Add(新系统.ComponentModel.SortDescription(“属性[_年]”,系统.ComponentModel.ListSortDirection.升序));
((ColumnSeries)MyChart2.Series[0]).ItemsSource=pair.Value;//new KeyValuePair[]{new KeyValuePair(20051200)};
}
}
}

谢谢

此代码应该可以帮助您

 bool SortDateAsc = true;
    private void Date_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (SortDateAsc)
        {
            ObservableCollection<InvoicesDTO> a = new ObservableCollection<InvoicesDTO>(((ResolutionVM)this.DataContext).MainInvoiceList.OrderBy(oc => oc.FC_AGE));
            ((ResolutionVM)this.DataContext).MainInvoiceList = a;
            InvoiceGrid.ItemsSource = ((ResolutionVM)this.DataContext).MainInvoiceList.ToList();
            SortDateAsc = false;
            ((ResolutionVM)this.DataContext).RefreshSelctedInvoice();
        }
        else
        {
            ObservableCollection<InvoicesDTO> a = new ObservableCollection<InvoicesDTO>(((ResolutionVM)this.DataContext).MainInvoiceList.OrderByDescending(oc => oc.FC_AGE));
            ((ResolutionVM)this.DataContext).MainInvoiceList = a;
            InvoiceGrid.ItemsSource = ((ResolutionVM)this.DataContext).MainInvoiceList.ToList();
            SortDateAsc = true;
            ((ResolutionVM)this.DataContext).RefreshSelctedInvoice();
        }

    }
bool-SortDateAsc=true;
私有作废日期\u MouseLeftButtonDown(对象发送者,MouseButtonEventArgs e)
{
if(SortDateAsc)
{
ObservableCollection a=新的ObservableCollection(((ResolutionVM)this.DataContext).MainInvoiceList.OrderBy(oc=>oc.FC_年龄));
((ResolutionVM)this.DataContext).MainInvoiceList=a;
InvoiceGrid.ItemsSource=((ResolutionVM)this.DataContext).MainInvoiceList.ToList();
SortDateAsc=false;
((ResolutionVM)this.DataContext).RefreshSelctedInvoice();
}
其他的
{
ObservableCollection a=新的ObservableCollection(((ResolutionVM)this.DataContext).MainInvoiceList.OrderByDescending(oc=>oc.FC_年龄));
((ResolutionVM)this.DataContext).MainInvoiceList=a;
InvoiceGrid.ItemsSource=((ResolutionVM)this.DataContext).MainInvoiceList.ToList();
SortDateAsc=true;
((ResolutionVM)this.DataContext).RefreshSelctedInvoice();
}
}
 private void SelectedRoadsTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    if (e.OldValue != null)
    {
        Graphic g = e.OldValue as Graphic;
        g.UnSelect();
        g.SetZIndex(0);
    }

    if (e.NewValue != null)
    {
        Graphic g = e.NewValue as Graphic;
        g.Select();
        g.SetZIndex(1);

        //Relationship query
        RelationshipParameter relationshipParameters = new RelationshipParameter()
        {
            ObjectIds = new int[] { Convert.ToInt32(g.Attributes[SelectedRoadsTreeView.Tag as string]) },
            OutFields = new string[] { "loc_id, count_id, _year, adt" },
            RelationshipId = 0,
            OutSpatialReference = Map.SpatialReference
        };

        queryTask.ExecuteRelationshipQueryAsync(relationshipParameters);

        MyChart2.Title = g.Attributes["TrafficCounts.COUNTUSER.%segment_master._street"];
    }
}
void QueryTask_ExecuteRelationshipQueryCompleted(object sender, RelationshipEventArgs e)
{
    RelationshipResult pr = e.Result;
    if (pr.RelatedRecordsGroup.Count == 0)
    {
        RelatedRowsDataGrid.ItemsSource = null;
    }
    else
    {
        foreach (var pair in pr.RelatedRecordsGroup)
        {
            RelatedRowsDataGrid.ItemsSource = pair.Value;
            //?.SortDescriptions.Add(new System.ComponentModel.SortDescription("Attributes[_year]", System.ComponentModel.ListSortDirection.Ascending));

            ((ColumnSeries)MyChart2.Series[0]).ItemsSource = pair.Value;    //new KeyValuePair<int, int>[] { new KeyValuePair<int, int>(2005, 1200) };
        }
    }
}
 bool SortDateAsc = true;
    private void Date_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (SortDateAsc)
        {
            ObservableCollection<InvoicesDTO> a = new ObservableCollection<InvoicesDTO>(((ResolutionVM)this.DataContext).MainInvoiceList.OrderBy(oc => oc.FC_AGE));
            ((ResolutionVM)this.DataContext).MainInvoiceList = a;
            InvoiceGrid.ItemsSource = ((ResolutionVM)this.DataContext).MainInvoiceList.ToList();
            SortDateAsc = false;
            ((ResolutionVM)this.DataContext).RefreshSelctedInvoice();
        }
        else
        {
            ObservableCollection<InvoicesDTO> a = new ObservableCollection<InvoicesDTO>(((ResolutionVM)this.DataContext).MainInvoiceList.OrderByDescending(oc => oc.FC_AGE));
            ((ResolutionVM)this.DataContext).MainInvoiceList = a;
            InvoiceGrid.ItemsSource = ((ResolutionVM)this.DataContext).MainInvoiceList.ToList();
            SortDateAsc = true;
            ((ResolutionVM)this.DataContext).RefreshSelctedInvoice();
        }

    }