【发布时间】:2017-02-06 17:10:14
【问题描述】:
我试图在 ListBox 中放置一个 ItemsControl,但内部 ItemsControl 的数据绑定存在问题。
EF 型号:
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