0

我创建了一个 XamGrid,其中我有一个三层层次结构。我在silverlight 4 中创建了它。我在xamgrid 上方有一个搜索文本框,它是一个自动完成框。当我从 AutocompleteBox 中选择一个项目并按下回车键时,我希望 Grid 中的该项目得到扩展。我怎样才能做到这一点??请建议..我的自动完成 bos 是:

<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>
4

1 回答 1

1

XamGrid 有一个 Rows 属性,它是所有根级别行的集合。您可以遍历这些行及其子行,以查找在自动完成框中搜索的数据。找到数据后,您可以将相应行的 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;
}
于 2013-01-11T16:26:51.933 回答