WPF EF数据绑定:ListBox中的ItemsControl(主要细节)

时间:2017-02-06 17:10:14

标签: c# wpf entity-framework binding

我正在尝试将ItemsControl放在ListBox中,但是我对内部ItemsControl的数据绑定有问题。

EF模型:

EF Database Model

EmployeeViewModel:

公共类EmployeeViewModel {     public ListCollectionView Employees {get;组; }

public EmployeeViewModel()
{
    LoadData();
}

private void LoadData()
{
    using (testdbEntities context = new testdbEntities())
    {
        IEnumerable<Employees> query = (from e in context.Employees
                                        orderby e.Lastname
                                        select e);

        ObservableCollection<Employees> emp = new ObservableCollection<Employees>(query);
        Employees = new ListCollectionView(emp);
    }
}

}

查看:

<Window x:Class="Employee.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Employee"
        xmlns:viewModel="clr-namespace:Employee.ViewModel"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <viewModel:EmployeeViewModel/>
    </Window.DataContext>

    <Grid>
        <ListBox x:Name="listEmployees"
                 ItemsSource="{Binding Employees}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock>
                            <Run Text="{Binding Lastname}"/>,
                            <Run Text="{Binding Firstname}"/>
                        </TextBlock>
                        <ItemsControl Name="empSkills" ItemsSource="{Binding Skills}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Skill}"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

ListBox绑定完美,但内部ItemsControl没有绑定。

可能是什么问题? 任何帮助表示赞赏。

输出: System.Windows.Data错误:17:无法从''(类型'Employees_463D630561C8612AEA43DCC9EC6E2ACDC644CFB28D50978184F80EEEE86D779E')获取'技能'值(类型'ICollection`1')。 BindingExpression:路径=技能; DataItem ='Employees_463D630561C8612AEA43DCC3EC6E2ACDC644CFB28D50978184F80EEEE86D779E'(HashCode = 29719745); target元素是'ItemsControl'(Name ='');目标属性是 '的ItemsSource'(类型 '的IEnumerable')TargetInvocationException:'System.Reflection.TargetInvocationException:明镜Eigenschaftenaccessor技能献给DAS System.Data.Entity.DynamicProxies.Employees_463D630561C8612AEA43DCC3EC6E2ACDC644CFB28D50978184F80EEEE86D779E-OBJEKT帽子folgende Ausnahme verursacht:模具的ObjectContext-INSTANZ wurde verworfen UND卡恩nichtmehrfürVorgängeverwendetwerden,fürdieeine Verbindung erforderlich ist。 ---&GT; System.ObjectDisposedException:Die ObjectContext-Instanz wurde verworfen und kann nichtmehrfürVorgängeverwendetwerden,fürdieeine Verbindung erforderlich ist。

2 个答案:

答案 0 :(得分:0)

Firstname和Lastname的属性肯定在内部视图模型中,而不是Employees的主视图模型?你不必将datacontext更改为内部/详细视图模型吗?

答案 1 :(得分:0)

阅读输出让我得到了解决方案。谢谢林恩的暗示。

问题是在LoadData()中使用using块。可以加载员工数据,但可以防止内部ItemsControl中延迟加载技能数据。

我的EmployeeViewModel的新工作版

    public class EmployeeViewModel
    {
        private testdbEntities context = new testdbEntities();

        public ListCollectionView Employees { get; set; }

        public EmployeeViewModel()
        {
            LoadData();
        }

        private void LoadData()
        {
            IEnumerable<Employees> query = (from e in context.Employees
                                            orderby e.Lastname
                                            select e);

            ObservableCollection<Employees> emp = new ObservableCollection<Employees>(query);
            Employees = new ListCollectionView(emp);
        }

    }
}