0

我收到以下异常

NotSupportedException: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery)

堆栈跟踪:

   at System.Data.Entity.Infrastructure.DbQuery`1.System.ComponentModel.IListSource.GetList()
   at MS.Internal.Data.ViewManager.GetViewRecord(Object collection, CollectionViewSource cvs, Type collectionViewType, Boolean createView, Func`2 GetSourceItem)
   at MS.Internal.Data.DataBindEngine.GetViewRecord(Object collection, CollectionViewSource key, Type collectionViewType, Boolean createView, Func`2 GetSourceItem)
   at System.Windows.Data.CollectionViewSource.GetDefaultCollectionView(Object source, Boolean createView, Func`2 GetSourceItem)
   at System.Windows.Data.CollectionViewSource.GetDefaultCollectionView(Object source, DependencyObject d, Func`2 GetSourceItem)
   at System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable value, Func`2 GetSourceItem)
   at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
   at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.Activate(Object item)
   at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
   at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
   at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
   at MS.Internal.Data.DataBindEngine.Run(Object arg)
   at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
   at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.UIElement.UpdateLayout()

主窗口.xaml

<Window x:Class="TryingWPFWithUltimate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TryingWPFWithUltimate;assembly="
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>

    <Grid>    
        <ComboBox ItemsSource="{Binding Path=DatabaseContext.Schools}"> //Exception here
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Label Content="{Binding Path=Name}"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>            
    </Grid>
</Window>

视图模型.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TryingWPFWithUltimate
{
    class ViewModel:INotifyPropertyChanged
    {    
        private DatabaseContext _databaseContext;
        public DatabaseContext DatabaseContext { get { return _databaseContext; } set { _databaseContext = value; RaisePropertyChanged("DatabaseContext"); } }

        public ViewModel()
        {
            DatabaseContext = new DatabaseContext();
        }
    }
}

这是实体数据模型的外观(非常简单) -

模型

谁能解释我做错了什么以及如何解决这个问题?

4

1 回答 1

1

正如在另一篇文章中所解释的,您不能直接将数据绑定到 DbSet,这是您正在尝试做的事情。相反,您需要创建一个本地集合并使用查询结果填充它。

这是一个非常好的教程

您可以从以下视图模型的示例开始:

using System.Collections.ObjectModel;

namespace TryingWPFWithUltimate
{
    class ViewModel:INotifyPropertyChanged
    {    
        private DatabaseContext _databaseContext;

        public ViewModel()
        {
            _databaseContext = new DatabaseContext();
            Schools = new ObservableCollection<School>(_databaseContext.Schools);
        }

        public ObservableCollection<School> Schools { get; set; }

    }
}

和绑定:

<ComboBox ItemsSource="{Binding Path=Schools}">
于 2013-07-01T14:51:17.077 回答