实体框架绑定文本框文本未显示实体

时间:2017-07-13 11:14:41

标签: wpf entity-framework binding

我正在学习使用版本6和VS 2013更新5的实体框架。作为练习,我使用Northwind的Employees为人员数据创建了一个简单的联系人表单,并更改了实体生成代码以使用Employee的可观察集合。表单(此处简化)有一个DataGrid,显示名字和姓氏的人员和文本框。它们都具有与DataContext相同的CollectionViewSource。还有一个按钮可以通过重新加载表来还原更改。这工作正常,除非我还原更改 - 如果我更改文本框中的名称并还原更改而不在DataGrid中选择新行,更改的名称仍显示在文本框中。 DataGrid立即显示更改,当我选择新行时,文本框显示更改,这只是一件事。如何使文本框在DataContext中显示新数据而不向表单添加不必要的依赖项? 我确实检查过使用INotifyPropertyChanged解决了这个问题,但是我想在改变.tt文件之后不必再使用另一个kludge,比如PropertyChanged.Fody。

这是生成的员工

namespace EF_one_table
{
    using System;
    using System.Collections.ObjectModel;

    public partial class Employee
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Employee()
        {
            this.Employees1 = new ObservableCollection<Employee>();
        }

        public int EmployeeID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        ... the rest of the table's fields ....

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ObservableCollection<Employee> Employees1 { get; set; }
        public virtual Employee Employee1 { get; set; }
    }
}

XAML

<Window x:Class="EF_one_table.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:EF_one_table"
        Title="COPY 3" Height="299.179" Width="530.836" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
    <Window.Resources>
        <CollectionViewSource x:Key="peopleViewSource" Source="local:Employee" />
    </Window.Resources>
    <Grid DataContext="{StaticResource peopleViewSource}" Margin="0,0,0,0">
        <DataGrid Name="dg1" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="23,33,0,0" VerticalAlignment="Top" Height="215" Width="226" 
                 AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Name" Width="200">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Path=FirstName}" Width="Auto" />
                                <TextBlock Text=" "/>
                                <TextBlock Text="{Binding Path=LastName}" />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <TextBlock Margin="265,36,0,0" Text="First Name" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" />
        <TextBlock Margin="265,75,0,0" Text="Last Name" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" />
        <TextBox Height="23" Margin="351,36,0,0" Width="150" HorizontalAlignment="Left" VerticalAlignment="Top"
                 Text="{Binding Path=FirstName}" />
        <TextBox Height="23" Margin="351,75,0,0" Width="150" HorizontalAlignment="Left" VerticalAlignment="Top"
                 Text="{Binding Path=LastName}" />
        <Button x:Name="btnDiscard" Content="Discard All Changes" Margin="391,126,0,0"  Width="110" Click="btnDiscard_Click" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </Grid>
</Window>

和C#

    public partial class MainWindow : Window
    {
        private NorthwindEntities _context = new NorthwindEntities();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CollectionViewSource peopleViewSource = this.Resources["peopleViewSource"] as CollectionViewSource;
            _context.Employees.Load();
            peopleViewSource.Source = _context.Employees.Local;
        }

        private void btnDiscard_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Discard All Changes","Lose your work!",MessageBoxButton.YesNo,MessageBoxImage.Question,MessageBoxResult.No) == MessageBoxResult.Yes)
            {
                // expose the ObjectContext inside the DbContext instance for the Refresh() method
                var objContext = ((IObjectContextAdapter)_context).ObjectContext;
                // "Property changes made to objects in the object context will be replaced with values from the data source. 
                objContext.Refresh(System.Data.Entity.Core.Objects.RefreshMode.StoreWins, _context.Employees);
                dg1.Items.Refresh();
             }
        }
    }

0 个答案:

没有答案