C# 如何在MVVM中显示WPF数据网格中的数据

C# 如何在MVVM中显示WPF数据网格中的数据,c#,wpf,xaml,mvvm,datagrid,C#,Wpf,Xaml,Mvvm,Datagrid,我的xaml代码 <Window x:Class="SampleWPFApp.View.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewmodel="clr-namespace:SampleWPFApp.ViewM

我的xaml代码

<Window x:Class="SampleWPFApp.View.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewmodel="clr-namespace:SampleWPFApp.ViewModel"
        Title="Window1" Height="500" Width="500">
    <Window.Resources>
        <viewmodel:FirstPageViewModel x:Key="fp"></viewmodel:FirstPageViewModel>
    </Window.Resources>
<Grid>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="75,38,0,0" TextWrapping="Wrap" Text="{Binding FirstName, Source={StaticResource fp}}" VerticalAlignment="Top" Width="120"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="75,79,0,0" TextWrapping="Wrap" Text="{Binding LastName, Source={StaticResource fp}}" VerticalAlignment="Top" Width="120"/>
    <Button Content="Add" HorizontalAlignment="Left" Margin="104,118,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.093,1.636" Command="{Binding acomm, Source={StaticResource fp}}" />
    <DataGrid HorizontalAlignment="Left" Name="mydatagrid" Margin="47,194,0,0" VerticalAlignment="Top" Height="178" Width="385" ItemsSource="{Binding Path=MyDataGrid,Mode=TwoWay,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}" DataContext="{DynamicResource fp} " Loaded="DataGrid_Loaded"/>
</Grid>
但是,即使在设置了从数据库网格绑定到datagrid的列表的值之后,该列表在UI中也不会得到更新/刷新。请帮助。我还实现了INotifyPropertyChanged接口。

如果您查看您的ItemsSource绑定:

您会注意到绑定到的属性不存在。这应该是MySampleGrid

顺便说一句,绑定的大部分内容都可以省略,这就是所需的全部内容:

ItemsSource="{Binding MySampleGrid}"
设置窗口的DataContext也是有意义的,子元素将从中继承,剩余的DataContext属性可以删除。

您可以做一些事情来改进代码

首先,我建议您使用ObservableCollection,因为它 改变跟踪机制。 还将网格的DataContext设置为viewmodel,而不是 将单个控件的datacontext添加到viewmodel 将所有内容放在一起,您的代码显示现在如下所示:

     private ObservableCollection<Employee> mysampleGrid=new ObservableCollection<Employee>();

            public ObservableCollection<Employee> MysampleGrid
            {
                get { return mysampleGrid; }
                set { mysampleGrid= value; }            
            }

public void AddDataToDb()
    {            

        e.FirstName = FirstName;
        e.LastName = LastName;
        context.Employees.Add(e);
        context.SaveChanges();
        this.MySampleGrid = null;

        //Setting List to value from database after inserting into db
        var employees = context.Employees.Where(x => x.FirstName != null).ToList();
        foreach(var employee in employees)
        mysampleGrid.Add(employee);
    }
最后在你看来

<Window x:Class="SampleWPFApp.View.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewmodel="clr-namespace:SampleWPFApp.ViewModel"
        Title="Window1" Height="500" Width="500">
    <Window.Resources>
        <viewmodel:FirstPageViewModel x:Key="fp"></viewmodel:FirstPageViewModel>
    </Window.Resources>
<Grid DataContext="{StaticResource fp}">
            <TextBox HorizontalAlignment="Left" Height="23" Margin="75,38,0,0" TextWrapping="Wrap" Text="{Binding FirstName}" VerticalAlignment="Top" Width="120"/>
            <TextBox HorizontalAlignment="Left" Height="23" Margin="75,79,0,0" TextWrapping="Wrap" Text="{Binding LastName}" VerticalAlignment="Top" Width="120"/>
            <Button Content="Add" HorizontalAlignment="Left" Margin="104,118,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.093,1.636" Command="{Binding acomm}" />
            <DataGrid HorizontalAlignment="Left" Name="mydatagrid" Margin="47,194,0,0" VerticalAlignment="Top" Height="178" Width="385" ItemsSource="{Binding Path=MysampleGrid,Mode=TwoWay,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}"  Loaded="DataGrid_Loaded"/>

        </Grid>

谢谢你指出!。这是一个愚蠢的错误。另外,如果我删除DataContext属性,数据似乎不会出现。@sujai删除它并设置窗口的DataContext。只需移除它们就会破坏它。@Pamparanapa,Observable collection示例很好。但若我想从多个实体查询数据,只需将List更改为IEnumerable即可,但在这种情况下observableCollection不起作用,对吗?
ItemsSource="{Binding MySampleGrid}"
     private ObservableCollection<Employee> mysampleGrid=new ObservableCollection<Employee>();

            public ObservableCollection<Employee> MysampleGrid
            {
                get { return mysampleGrid; }
                set { mysampleGrid= value; }            
            }

public void AddDataToDb()
    {            

        e.FirstName = FirstName;
        e.LastName = LastName;
        context.Employees.Add(e);
        context.SaveChanges();
        this.MySampleGrid = null;

        //Setting List to value from database after inserting into db
        var employees = context.Employees.Where(x => x.FirstName != null).ToList();
        foreach(var employee in employees)
        mysampleGrid.Add(employee);
    }
<Window x:Class="SampleWPFApp.View.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewmodel="clr-namespace:SampleWPFApp.ViewModel"
        Title="Window1" Height="500" Width="500">
    <Window.Resources>
        <viewmodel:FirstPageViewModel x:Key="fp"></viewmodel:FirstPageViewModel>
    </Window.Resources>
<Grid DataContext="{StaticResource fp}">
            <TextBox HorizontalAlignment="Left" Height="23" Margin="75,38,0,0" TextWrapping="Wrap" Text="{Binding FirstName}" VerticalAlignment="Top" Width="120"/>
            <TextBox HorizontalAlignment="Left" Height="23" Margin="75,79,0,0" TextWrapping="Wrap" Text="{Binding LastName}" VerticalAlignment="Top" Width="120"/>
            <Button Content="Add" HorizontalAlignment="Left" Margin="104,118,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.093,1.636" Command="{Binding acomm}" />
            <DataGrid HorizontalAlignment="Left" Name="mydatagrid" Margin="47,194,0,0" VerticalAlignment="Top" Height="178" Width="385" ItemsSource="{Binding Path=MysampleGrid,Mode=TwoWay,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}"  Loaded="DataGrid_Loaded"/>

        </Grid>