【问题标题】:WPF DataGrid doesnt update after instert element to source将元素插入源后 WPF DataGrid 不更新
【发布时间】:2011-11-19 07:54:33
【问题描述】:

我是 WPF 的新手。我尝试做一个与我的实体框架模型表绑定的 DataGrid。我的表的名称是:单词。我正在尝试做简单的字典。 我正在使用 SQL Sever Compact、WPF、实体框架。

但是,从表中删除记录后,它工作正常...来自 DataGrid desapers 的行

你能告诉我为什么在将记录添加到实体后它们没有插入到 DataGrid 中吗? 关闭程序并再次打开后,记录在DataGrid中。

这是我的 DataGrid 窗口代码:

<Grid>

<Grid>
        <DataGrid IsReadOnly="True" AutoGenerateColumns="False" Name="dataGrid" DataContext="{Binding }" ItemsSource="{Binding}" Margin="0,90,0,0">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Sentence" Binding="{Binding Path=sentence}" />
                <DataGridTextColumn Header="Translation" Binding="{Binding Path=translation}" />
                <DataGridTemplateColumn Header="Operations">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Delete" Click="Button_Click" Tag="{Binding Path=id}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="sentence" VerticalAlignment="Top" Width="153" />
        <TextBox Height="23" Margin="171,12,131,0" Name="translations" VerticalAlignment="Top" AcceptsReturn="True" />
        <Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="90,41,0,0" Name="addBtn" VerticalAlignment="Top" Width="75" Click="addBtn_Click" />
    </Grid>

这是我的 C# 代码:

public partial class AddWordWnd : Window {
    static dbEntities db = new dbEntities();

    public AddWordWnd() {
        InitializeComponent();
        dataGrid.DataContext = db.Words;
        dataGrid.Visibility = System.Windows.Visibility.Visible;
    }

    private void Button_Click( object sender, RoutedEventArgs e ) {
        Button btn = sender as Button;
        System.Guid ii = System.Guid.Parse(btn.Tag.ToString());
        MessageBox.Show(ii.ToString());
        db.DeleteObject(db.Words.Single(w => w.id == ii));
        db.SaveChanges();
    }

    private void addBtn_Click( object sender, RoutedEventArgs e ) {
        System.Guid gg = System.Guid.NewGuid();
        db.Words.AddObject(new Words() { id = gg, sentence = translations.Text, translation = sentence.Text });
        db.SaveChanges();
        dataGrid.Items.Refresh();
    }
}

我的数据库表结构:

Words
---------------
id uniqueidentifier rowguidcol not null primary key,
sentence nvarchar(255) not null,
translation nvarchar(255) not null,

【问题讨论】:

    标签: c# .net wpf entity-framework data-binding


    【解决方案1】:

    我最好的猜测是 DataContext 被设置为您项目的副本,而不是指向现有项目。

    不是将DataContext 设置到一个项目,而是将它绑定到那个项目。这将使 DataContext 指向项目,而不是复制它。

    Binding b = new Binding();
    b.Source = db.Words;
    dataGrid.SetBinding(DataGrid.ItemsSourceProperty, b);
    

    编辑

    刚刚注意到您直接绑定到上下文。默认情况下,这不会引发 CollectionChanged 事件来告诉您的 DataGrid 项目已更改,因此 DataGrid 不会尝试重新读取它的 ItemsSource。

    尝试手动刷新绑定 (dataGrid.GetBindingExpression(DataGrid.ItemsSourceProperty).UpdateTarget()),或通过重新查询数据库来刷新上下文(参见 this answer 示例)

    【讨论】:

    • 感谢您的回复,但它仍然没有解决我的问题...:/
    • 刚刚注意到您直接绑定到上下文。默认情况下,这不会引发 CollectionChanged 事件来告诉您的 DataGrid 项目已更改,因此 DataGrid 不会尝试重新读取它的 ItemsSource。尝试在绑定上手动引发 PropertyChanged 事件
    • 如果你能告诉我我该怎么做就太好了?我是 WPF 的新手。这是我在 WPF 中写东西的第二天。
    • @nosbor 我相信它类似于BindingOperations.GetBindingExpressionBase(dataGrid, DataGrid.ItemsSourceProperty).UpdateTarget();
    • 我的猜测是,即使您添加了该项目,上下文也不会在您重新查询数据库之后更新。例如,请参见以下链接:stackoverflow.com/questions/3256245/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 2018-12-03
    • 2012-06-22
    • 2014-12-13
    • 1970-01-01
    相关资源
    最近更新 更多