2013-01-10 17 views
0

我創建了一個XamGrid,其中我有一個三級分層結構。我已經在silverlight 4中創建了它。我在xamgrid上面有一個搜索文本框,它是一個自動完成框。當我從AutocompleteBox中選擇一個項目並且輸入priess時,我想讓網格中的項目得到擴展。我怎樣才能做到這一點?? PLZ建議..我的自動完成BOS是:當在搜索菜單中輸入項目時,展開xamgrid項目

<local:ExtendedAutoCompleteBox 
          x:Name="InvNamesSearch" 
          WaterMark="TypeName" 
          HorizontalAlignment="Left" VerticalAlignment="Center" 
          Width="300" Margin="5,0,0,0" 
          MinimumPrefixLength="3" 
          IsTextCompletionEnabled="False" 
          Text="{Binding InvestmentText, Mode=TwoWay}" 
          ItemsSource="{Binding A,Mode=OneWay}" 
          SelectedItem="{Binding Path=B, Mode=TwoWay}"             
          ValueMemberBinding="{Binding A}"        
          FilterMode="Contains" Canvas.Left="683" Canvas.Top="9" 
          Command="{Binding AutoSuggestEnterCommand}"> 
        <local:ExtendedAutoCompleteBox.ItemTemplate> 
         <DataTemplate> 
          <TextBlock x:Name="txtAutoSelectedItem" Text="{Binding A}" /> 
         </DataTemplate> 
        </local:ExtendedAutoCompleteBox.ItemTemplate> 
       </local:ExtendedAutoCompleteBox> 

回答

1

的XamGrid有一個行屬性,是一切的根源水平行的集合。您可以遍歷這些行及其子行,以查找在自動填充框中搜索的數據。一旦找到數據,您可以在相應的行上將IsExpanded屬性設置爲true。代碼可能是這樣的:

// This function returns a row that contains the supplied search criteria 
public Row FindRowFromAutoCompleteBox(RowCollection rootRows, string searchCriteria) 
{ 
    foreach (Row row in rootRows) 
    { 
     if (row.Data is LevelOneDataType) 
     { 
      if ((row.Data as LevelOneDataType).LevelOneProperty == searchCriteria) 
       return row; 
     } 
     if (row.Data is LevelTwoDataType) 
     { 
      if ((row.Data as LevelTwoDataType).LevelTwoProperty == searchCriteria) 
       return row; 
     } 
     if (row.Data is LevelThreeDataType) 
     { 
      if ((row.Data as LevelThreeDataType).LevelThreeProperty == searchCriteria) 
       return row; 
     } 
     // Search child rows. 
     if (row.ChildBands.Count != 0) 
     { 
      Row result = FindRowFromAutoCompleteBox(row.ChildBands[0].Rows, searchCriteria); 
      if (result != null) 
       return result; 
     } 
    } 

    return null; 
} 

// Walks up the hierarchy starting at the supplied row and expands parent rows as it goes. 
public void ExpandHierarchy(Row row) 
{ 
    Row parentRow = null; 

    // The row is a child of another row. 
    if (row.ParentRow is ChildBand) 
     parentRow = (row.ParentRow as ChildBand).ParentRow; 

    while (parentRow != null) 
    { 
     // Expand the row. 
     parentRow.IsExpanded = true; 

     if (parentRow.ParentRow is ChildBand) 
      parentRow = (parentRow.ParentRow as ChildBand).ParentRow; 
     else 
      parentRow = null; 
    } 
} 

有了這些功能,您現在可以搜索並展開要行。

Row result = FindRowFromAutoCompleteBox(xamGrid1.Rows, "Value"); 
if (result != null) 
{ 
    ExpandHierarchy(result); 
    result.IsSelected = true; 
}