0
votes

I have to use a list.

I'm trying to add my searched data to the view list and it's crashing again and again.

i thing the issue is in list view Thank you.

Error:

System.Exception: 'Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))'

XML:

        <ListView  x:Name="listView" Grid.ColumnSpan="5" Grid.RowSpan="5" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Top" Height="421" Width="711" Margin="0,93,0,0" Grid.Column="1">
        <ListView.HeaderTemplate>
            <DataTemplate>
                <Grid Padding="12" Background="{ThemeResource SystemBaseLowColor}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="200"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="Name" Style="{ThemeResource CaptionTextBlockStyle}"/>
                    <TextBlock Grid.Column="1" Text="Publisher" Style="{ThemeResource CaptionTextBlockStyle}"/>
                    <TextBlock Grid.Column="2" Text="PublishDate" Style="{ThemeResource CaptionTextBlockStyle}"/>
                    <TextBlock Grid.Column="3" Text="Quantity" Style="{ThemeResource CaptionTextBlockStyle}"/>
                    <TextBlock Grid.Column="4" Text="Category" Style="{ThemeResource CaptionTextBlockStyle}"/>
                    <TextBlock Grid.Column="5" Text="Price" Style="{ThemeResource CaptionTextBlockStyle}"/>
                </Grid>
            </DataTemplate>
        </ListView.HeaderTemplate>
        <ListView.ItemTemplate>
            <DataTemplate x:Name="TableDataTemplate">
                <Grid Height="48" AutomationProperties.Name="{Binding Name}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0"/>
                        <ColumnDefinition Width="200"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock x:Name="nameTbx" Grid.Column="1" VerticalAlignment="Center" Text="{Binding Name}" />
                    <TextBlock Grid.Column="2" VerticalAlignment="Center" Text="{Binding model.Publisher}"/>
                    <TextBlock Grid.Column="3" VerticalAlignment="Center" Text="{Binding model.PublishDate}"/>
                    <TextBlock Grid.Column="4" VerticalAlignment="Center" Text="{Binding model.Quantity}"/>
                    <TextBlock Grid.Column="5" VerticalAlignment="Center" Text="{Binding model.Category}"/>
                    <TextBlock Grid.Column="6" VerticalAlignment="Center" Text="{Binding model.Price}"/>

                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

c#:

 {
    public MainPage()
    {
        this.InitializeComponent();


        listView.ItemsSource = BookLib.ItemCollection.Items;   


    }


    private void searchTbx_TextChanged(object sender, TextChangedEventArgs e)
    {
        var match = BookLib.ItemCollection.Items.Find(x => x.Name.ToLower() == searchTbx.Text.ToLower());
        if (match != null)
        {
            listView.Items.Add(match.Name);
        }
    }

please help me to slove this issue

1

1 Answers

2
votes

You set the ItemsSource to BookLib.ItemCollection.Items in the constructor, means that, all the elements of BookLib.ItemCollection.Items are now items of that listView.

So, in the TextChanged event, aren't you trying to add the same Item which is already in the listView?

That's why your app is crashing.

Solution: Remove this line from the constructor:

listView.ItemsSource = BookLib.ItemCollection.Items;

You didn't mention the ViewModel you are using for the listview. I am assuming It's like this:

public class Book
{
    public string Name { get; set; }
    public string Publisher { get; set; }
    ………
    ………
    ………
}

Then you should mention the Data type in the ItemTemplate of your listView like this:

<ListView.ItemTemplate>
        <DataTemplate x:Name="TableDataTemplate" x:DataType="Book">
            <Grid …………
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>

this should solve your problem.

Note: If you want to let the user search through the BookLib.ItemCollection.Items, consider using an AutoSuggestBox.