2012-10-31 41 views
0

我使用http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#QueryRelatedRecords的示例代码将相关数据显示到数据网格中。在我的数据中,我需要按年排序表(我的相关表中名为“_year”的属性)。我不知道如何完成这个 - 在xaml或c#中。下面是我当前的代码:Silverlight 4 - 相关表datagrid显示排序

XAML:

   <basics:TabControl Grid.Column="1" Margin="0,-1,0,0" Grid.RowSpan="2" Grid.ColumnSpan="2" Grid.Row="1" d:LayoutOverrides="GridBox"> 
       <basics:TabItem x:Name="DataTab" Header="Data"> 
        <Grid Background="#FFE5E5E5"> 
         <slData:DataGrid x:Name="RelatedRowsDataGrid" AutoGenerateColumns="False" HeadersVisibility="All" 
        BorderThickness="1" HorizontalScrollBarVisibility="Hidden" IsReadOnly="True" Margin="4,0,2,6" Height="127" VerticalAlignment="Bottom"> 
          <slData:DataGrid.Columns> 
           <slData:DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[loc_id]}" Header="Location ID" FontWeight="Bold"/> 
           <slData:DataGridTextColumn CanUserSort="True" Binding="{Binding Attributes[_year]}" Header="Year"/> 
           <slData:DataGridTextColumn CanUserSort="False" Binding="{Binding Attributes[adt]}" Header="ADT"/> 
          </slData:DataGrid.Columns> 
         </slData:DataGrid> 
        </Grid> 
       </basics:TabItem> 
       <basics:TabItem x:Name="ChartTab" Header="Chart"> 
        <Grid Background="#FFE5E5E5"> 
         <toolkit:Chart x:Name="MyChart2" Title="{Binding [PAGsde.DBO.TrafficCountNetwork.loc_id]}" 
          esri:GraphicsLayer.MapTipHideDelay="0:0:3" Background="LightGray" 
          LegendStyle="{StaticResource BlankLegendStyle}" TitleStyle="{StaticResource SmallTitleStyle}"> 
          <toolkit:Chart.Series> 
           <toolkit:ColumnSeries Title="ADT" IndependentValueBinding="{Binding Attributes[_year]}" 
              DependentValueBinding="{Binding Attributes[adt]}"> 
           </toolkit:ColumnSeries> 
          </toolkit:Chart.Series> 
         </toolkit:Chart> 
        </Grid> 
       </basics:TabItem> 
      </basics:TabControl> 

C#:

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) }; 
     } 
    } 
} 

谢谢!

回答

1

此代码应帮助您可

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(); 
     } 

    }