简体   繁体   中英

EF5 + ListView and Entities

I have the following classes in my project,

public class Area
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public ObservableCollection<VisitDetail> VisitDetails { get; set; }
}

public class VisitDetail
{
    [Key]
    public int VisitId { get; set; }
    [Required]
    public int AreaId { get; set; }

    public Area Area { get; set; }
}

The user wants to save their visited areas for the date using following method.

在此处输入图片说明

I want to get only the selected Areas from that ListView for saving them. When I'm trying to get those using ListView.Items[index].IsSelected it throws an error saying,

Unable to cast object of type 'Namespace.Area' to type 'System.Windows.Controls.ListViewItem'

Please tell me the exact way to solve my problem.

Edit 1 :

My project is in WPF. And note this as well, when the Visit Detail window loaded, the Area entity collection is bounded to the ListView.ItemsSource . (Because of WPF there are no any ListView.CheckedItems :( )

Edit 2 :

Thanks @blins your solution worked. But I can't get the checked items. I am posting my xaml here. However I can get selected items. If I could get those checked items, it would be a pleasure to me.

This is my XAML of the ListView

<ListView Name="lvList" SelectionMode="Multiple" ClipToBounds="True" >
    <ListView.View>
        <GridView >
            <GridViewColumn DisplayMemberBinding="{Binding Id}" />
            <GridViewColumn Header="Area" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate >
                        <CheckBox x:Name="checkbox" Content="{Binding Name}" IsChecked="{Binding Path=IsSelected}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

I think there should be a solution for my issue.

The exception is thrown because the Items property of your ListView actually represents the list of items used to generate the content of the list (ie, they are NOT of type ListViewItem but rather your Area type). This is just the way WPF operates, what you need in this case is to ask the underlying ItemsControl's ItemContainerGenerator for the ListViewItem that corresponds to the item in the UI.

XAML:

<ListView Name="listView1" />

Code (Assuming you have already set the DataContext and ItemsSource of the ListView, etc):

ListViewItem item = listView1
   .ItemContainerGenerator
   .ContainerFromIndex(0) as ListViewItem;

item.IsSelected = true;

And an alternative if you don't know/care about the index in the list but rather you know the item.

ListViewItem item = listView1
   .ItemContainerGenerator
   .ContainerFromItem(someAreaInstance) as ListViewItem;

Answer to Question Part 2:

You really should look deeper at the MVVM pattern which would help you avoid this scenario to begin with but here is one way to do what you are after. First you'll probably want a helper method something like the following (not tested):

static FrameworkElement GetVisualDescendantByName(DependencyObject control, string name)
{
    // Recurse
    FrameworkElement el = null;
    int nChildren = VisualTreeHelper.GetChildrenCount(control);
    for (int i = 0; i < nChildren; i++)
    {
        var child = VisualTreeHelper.GetChild(control, i);
        el = GetVisualDescendantByName(child, name);
        if (el != null)
            return el;
    }

    // See if control is a match
    if (control is FrameworkElement)
    {
        el = control as FrameworkElement;
        if (el.Name == name)
            return control as FrameworkElemdent;
    }

    return null;
}

The you could do something like...

foreach (var item in lvList.Items)
{
    var listItem = lvList.ItemContainerGenerator
        .ContainerFromItem(item) as ListViewItem;

    CheckBox cb = GetVisualDescendantByName(listItem, "checkbox") as CheckBox;

    // Do stuff with CheckBox...
    var myVar = cb.IsChecked;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM