Reputation: 1
I have created a XamGrid in which i have a three level hierarchy. i have created it in silverlight 4. I have a search textbox above the xamgrid, which is an autocompleteBox. when i select an item from the AutocompleteBox and priess enter, i want that item in the Grid to get expanded. How can i do that?? plz suggest.. my autocomplete bos is:
<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>
Upvotes: 0
Views: 718
Reputation: 156
The XamGrid has a Rows property that is a collection of all the root level rows. You can iterate over these rows and their child rows looking for the data that was searched for in the autocomplete box. Once you have found the data you can set the IsExpanded property to true on the respective row. The code might look like this:
// 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;
}
}
With these functions you can now search and expand to the row you want.
Row result = FindRowFromAutoCompleteBox(xamGrid1.Rows, "Value");
if (result != null)
{
ExpandHierarchy(result);
result.IsSelected = true;
}
Upvotes: 1