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

时间:2016-08-10 14:21:26

标签: c# wpf xaml mvvm datagrid

我的xaml代码

body.style.setProperty("--color", color);

我在ViewModel中绑定网格的属性

<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>

在ViewModel中单击按钮时调用的方法,

private List<Employee> mysampleGrid;
public List<Employee> MySampleGrid
{
    get
    {
        return mysampleGrid;
    }
    set
    {
        mysampleGrid = value;
        OnPropertyChanged("MySampleGrid");
    }
}

但是,即使在从数据库网格设置list(绑定到datagrid)的值之后,也没有在UI.please帮助中更新/刷新。我也实现了INotifyPropertyChanged接口。

2 个答案:

答案 0 :(得分:2)

如果你看一下ItemsSource绑定:

ItemsSource="{Binding Path=MyDataGrid,Mode=TwoWay,NotifyOnSourceUpdated=True,NotifyOnTargetUpdated=True}"

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

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

ItemsSource="{Binding MySampleGrid}"

设置DataContext的{​​{1}}也是有意义的,子元素将从此继承,剩余的Window属性可以删除。

答案 1 :(得分:2)

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

  1. 首先我会建议你使用ObservableCollection 改变跟踪机制。
  2. 还要将网格的DataContext设置为viewmodel而不是 单个控件的viewacontext到您的viewmodel
  3. 将所有代码放在一起显示现在看起来像这样:

         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>