【问题标题】:WPF EF Databinding: ItemsControl inside ListBox (master detail)WPF EF 数据绑定:ListBox 内的 ItemsControl(主详细信息)
【发布时间】:2017-02-06 17:10:14
【问题描述】:

我试图在 ListBox 中放置一个 ItemsControl,但内部 ItemsControl 的数据绑定存在问题。

EF 型号:

EF Database Model

EmployeeViewModel:

公共类 EmployeeViewModel { 公共 ListCollectionView 员工 { 获取;放; }

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_463D630561C8612AEA43DCC3EC6E2ACDC644CFB28D50978184F80EEEE86D779E”)获取“技能”值(类型“ICollection`1”)。 BindingExpression:路径=技能; DataItem='Employees_463D630561C8612AEA43DCC3EC6E2ACDC644CFB28D50978184F80EEEE86D779E' (HashCode=29719745);目标元素是'ItemsControl'(名称='');目标属性是 '的ItemsSource'(类型 '的IEnumerable')TargetInvocationException:'System.Reflection.TargetInvocationException:明镜Eigenschaftenaccessor技能献给DAS System.Data.Entity.DynamicProxies.Employees_463D630561C8612AEA43DCC3EC6E2ACDC644CFB28D50978184F80EEEE86D779E-OBJEKT帽子folgende Ausnahme verursacht:模具的ObjectContext-INSTANZ wurde verworfen UND卡恩nicht mehr für Vorgänge verwendet werden, für die eine Verbindung erforderlich ist. ---> System.ObjectDisposedException: Die ObjectContext-Instanz wurde verworfen und kann nicht mehr für Vorgänge verwendet werden, für die eine Verbindung erforderlich ist.

【问题讨论】:

  • 让我们看看输出窗口中的信息。如果存在绑定错误,那应该会尖叫。请Edit您的问题并包括在内。

标签: c# wpf entity-framework binding


【解决方案1】:

Firstname 和 Lastname 的属性肯定在内部视图模型中,而不是员工的主视图模型中?您不必将数据上下文更改为内部/详细视图模型吗?

【讨论】:

  • ItemsControl 固有地为子项设置了一个新的 DataContext。
【解决方案2】:

阅读输出使我找到了解决方案。感谢林恩的提示。

问题在于 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);
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2016-08-16
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    相关资源
    最近更新 更多