Delete Mutiple ListView SelectedItems in code behind with Entity Framework Core

0

I have a ListView that displays TitleName from Title table\entity. Upon ListView_OnSelectionChanged, I can Linq query and display a TitlesDetails Panel, and make visible another panel for edit and delete. But I can only delete one item. I've google and tried all I could till frustrated and backed off for few days. Truly, I'd be happy just to get the Multiple SelectedItems from the ListView into just any list/collection, so I can then do whatever with multiple. But if someone showed me how to just get the selectedItems from ListView, I can delete them. I just need them out of the ListView and put into a list/collection I can work with. I could then understand the thing 'I clearly don't know' Can't Cast from this, can't cast to that. Too many variations I've tried to pin point, as I may be using wrong approach all together.

Ok, Mr. ListView can I just please have the SelectedItems and show me how to just put them in any kind of list? So I can play with them from there? Delete them, manipulate them, even add them to a TitlePlayList table someday!

Xaml:

<ListView x:Name="ShowTitlesListView" Background="WhiteSmoke" 
                  MinHeight="10" Height="200" MaxHeight="500"
                  Margin="5 7 5 3" 
                  DataContext="{Binding Title}"
                  SelectionMode="Extended"
                  KeyUp="ShowTitlesListView_KeyUp"
                  SelectionChanged="ShowTitlesListView_OnSelectionChanged">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0 0">
                        <StackPanel Orientation="Horizontal" Margin="0 1">
                            <TextBlock x:Name="tbTitleName" Text="{Binding TitleName}"
                                       FontSize="14"
                                       TextWrapping="NoWrap"                                                                      
                                       Margin="2 1"/>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

My Model if needed to see:

...DB Context...  
public DbSet<User> Users { get; set; }
public DbSet<Title> Titles { get; set; }

 ...Tables\Entities...

public class User
{
        public int UserId { get; set; }
        public string UserLogin { get; set; }
        public string UserPwd { get; set; }      
        public List<Title> Titles { get; set; } 
        public override string ToString()
        {         
            return UserId + " " + UserLogin + " ";
        }
}
public class Title
{
        public int TitleId { get; set; }
        public string TitleName { get; set; }
        public string TtsRaw { get; set; }
        public string DirPath { get; set; }
        public int UserId { get; set; }
        public User User { get; set; }
        public override string ToString()
        {          
            return UserId + TitleId + TitleName;
        }
}

my Code Behind - * It is clear I'm missing a concept, so keep that in mind if you would!

EDIT: Adding -The Listview.ItemSource is in Page_Loaded(...) as below

    //LOADING DATA from Database Provider - SQLite - EF Core 5
    private void Page_Loaded(object sender, RoutedEventArgs e)
    {             
        #region Show current users titles
        using (var context = new PRSappContext())
        {
            var usersTitles =
               from t in context.Titles
               where t.UserId == CurrentUserId
               orderby t.TitleId descending
               select t;

            List<Title> selectedUsersTitles = usersTitles.ToList(); 

            ShowTitlesListView.ItemsSource = selectedUsersTitles;
        }

...

    private void ShowTitlesListView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        if (ShowTitlesListView.SelectedItems.Count > 1)
        {
            ((Title)ShowTitlesListView.SelectedItems);

When I try above line, I get below exception 'Exception User-Handlded' System.InvalidCastException HResult=0x80004002 Message=Unable to cast COM object of type 'System.__ComObject' to class type 'PRSapp.Model.Title'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface. Source=PRSapp.UWP StackTrace: at PRSapp.UWP.MainPage.ShowTitlesListView_OnSelectionChanged(Object sender, SelectionChangedEventArgs e) in C:\Users\Flazz\source\repos\FebOn\00MarchRepo\PRSapp\PRSapp.UWP\MainPage.xaml.cs:line 412

        if (ShowTitlesListView.SelectedItems.Count > 0)
        {
            Title selectedTitleID = ((Title)ShowTitlesListView.SelectedItem);
            int _selectedTitleID = selectedTitleID.TitleId;
            //Get public Title Id for Deleting a single and user forgets to select it
            SelectedTitleId = selectedTitleID.TitleId;

            using (var context = new PRSappContext())
            {
                var usersTitleDetails =
                   from t in context.Titles
                   where t.TitleId == selectedTitleID.TitleId
                   select t;

                List<Title> selectedUsersTitles = usersTitleDetails.ToList();

                TitleDetailsListView.ItemsSource = selectedUsersTitles;
                TitleDetailsListView.SelectedIndex = 0;
            }

        }

}

c#
listview
uwp
entity-framework-core
asked on Stack Overflow Feb 12, 2019 by user6183057 • edited Mar 7, 2019 by user6183057

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0