无法隐藏Listview项目

时间:2014-09-18 10:12:59

标签: c# xaml windows-8.1 win-universal-app

我正在开发通用Windows应用程序。它包含一个ListView,用于显示一组术语及其定义。 ListView绑定到XAML部分中的DataSource.cs文件。 ListView正确显示。

在键入关键字时,需要添加一个搜索框来过滤掉ListView项目。我已经确认搜索框能够使用的Debug.WriteLine("&#34)即可。

我面临的问题是我无法隐藏搜索框未选中的项目。实际搜索是在包含 if 块的 foreach 循环内部进行的。因此,我的想法是在相应的 else 块中隐藏 ListView.slectedItem 。 麻烦的是这种方法引发了一个例外,

  

类型' System.Exception'的第一次机会异常。发生在mscorlib.dll中   类型' System.Exception'的例外情况发生在mscorlib.dll但未在用户代码中处理   附加信息:灾难性故障(HRESULT异常:0x8000FFFF(E_UNEXPECTED))

     

该程序' XXXX.exe'退出时使用代码-1(0xffffffff)。

这里我提供了代码。

XAML

<ListView x:Name="ItemListView" 
    Margin="0,0,0,4" 
    Height="Auto" 
    ItemTemplate="{StaticResource MessageListImageTemplate}" 
    ShowsScrollingPlaceholders="False"
    ContainerContentChanging="ItemListView_ContainerContentChanging"
    ItemClick="ItemListView_ItemClick"        
    IsItemClickEnabled="True" 
    SelectionMode="None">

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="Padding" Value="0,0,0,20"/>
            </Style>
        </ListView.ItemContainerStyle>

</ListView>

<SearchBox  
    x:Name="search" 
    PlaceholderText="Search for a term"  
    SuggestionsRequested="search_SuggestionsRequested" 
    SearchHistoryEnabled="False" `
    Grid.Row="1"
    HorizontalAlignment="Left" 
    VerticalAlignment="Center" 
    Margin="50,0,0,0" 
    Height="50" />

C#

StoreData storeData = null;

public MainPage()
{
    storeData = new StoreData();

    //setting the ListView source to the sample data 
    ItemListView.ItemsSource = storeData.Collection;

    // making sure the first item is the selected item
    ItemListView.SelectedIndex = 0;           
}

void ItemListView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
    ItemViewer iv = args.ItemContainer.ContentTemplateRoot as ItemViewer;

    if (args.InRecycleQueue == true)
    {
        iv.ClearData();
    }
    else if (args.Phase == 0)
    {
        iv.ShowTitle(args.Item as Item);

        // Register for async callback to visualize Title asynchronously
        args.RegisterUpdateCallback(ContainerContentChangingDelegate);
    }
    else if (args.Phase == 1)
    {
        iv.ShowDescription();
        args.RegisterUpdateCallback(ContainerContentChangingDelegate);
    }

    // For imporved performance, set Handled to true since app is visualizing the data item
    args.Handled = true;
}

private TypedEventHandler<ListViewBase, ContainerContentChangingEventArgs> ContainerContentChangingDelegate
{
    get
    {
        if (_delegate == null)
        {
            _delegate = new TypedEventHandler<ListViewBase, ContainerContentChangingEventArgs>                (ItemListView_ContainerContentChanging);
        }
            return _delegate;
    }
}

private TypedEventHandler<ListViewBase, ContainerContentChangingEventArgs> _delegate;

private void search_SuggestionsRequested(Object sender, SearchBoxSuggestionsRequestedEventArgs e)
{

    IEnumerable<Item> suggestionList = (IEnumerable<Item>)storeData.Collection.Cast<Item>();

    string queryText = e.QueryText;

    if (!string.IsNullOrEmpty(queryText))
    {

        Windows.ApplicationModel.Search.SearchSuggestionCollection suggestionCollection = e.Request.SearchSuggestionCollection;

        foreach (Item suggestedItem in suggestionList)
        {
            String suggestion = suggestedItem.Title;
            Debug.WriteLine(suggestion);

            if (suggestion.StartsWith(queryText, StringComparison.CurrentCultureIgnoreCase))
            {
                suggestionCollection.AppendQuerySuggestion(suggestion);

            }

            else
            {
                 //this is the line throwing the exception     
                 ItemListView.Items.RemoveAt(ItemListView.SelectedIndex); 
            }
        }
    }
}

我是Windows开发的新手。任何和所有帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

这一定很快,因为我即将离开。简而言之,您需要两个集合来过滤数据,一个原始的,未触摸的集合和一个要显示的过滤集合。假设您将数据绑定到名为string的{​​{1}}属性到UI(用户键入的属性以过滤集合)。

您可以使用此FilterText使用string来过滤集合,如下所示:

LinQ

将此代码放入方法并从if (FilterText == string.Empty) FilteredCollection = new ObservableCollection<strging>(OriginalCollection); else FilteredCollection = new ObservableCollection<strging>( OriginalCollection.Where(s => s.StartsWith(FilterText))); 调用它,以便在键入每个字符后更新集合。当然,您需要将FilterText setter上的Binding.UpdateSourceTrigger属性设置为FilterText Binding,以使其按预期工作。