WPF MVVM DataGrid刷新数据库表中的新行

时间:2015-08-04 12:32:36

标签: c# wpf mvvm datagrid

我想从另一个应用程序添加新行到数据库表时自动刷新DataGrid。我正在使用ObservableCollection,但它不起作用。我正在使用EntitityFramework,代码优先。请帮我!

查看:

xmlns:wm="clr-namespace:Admin.ViewModels"

<Window.DataContext>
        <wm:IncidentViewModel/>
    </Window.DataContext>

<DataGrid
                x:Name="DataGridIncidenty" 
                Grid.Column="0" 
                Grid.Row="1" 
                AutoGenerateColumns="False"
                ColumnWidth="*"
                MouseDoubleClick="Row_DoubleClick"
                HorizontalAlignment="Stretch" 
                Margin="5,5,5,0"
                IsReadOnly="True"
                ColumnHeaderStyle="{StaticResource {ComponentResourceKey 
                                   TypeInTargetAssembly={x:Type filter:DataGridHeaderFilterControl}, 
                                   ResourceId=DataGridHeaderFilterControlStyle}}"
                filter:DataGridExtensions.IsClearButtonVisible="False"
                ItemsSource="{Binding Incidents, UpdateSourceTrigger=PropertyChanged}"
                SelectedIndex="0">                
            <DataGrid.Columns>
                <DataGridTextColumn Header="HostName" Binding="{Binding Computer.Hostname}" Width="auto"/>
                <DataGridTextColumn Header="Description" Binding="{Binding TextIncident}" Width="*">
            </DataGrid.Columns>
        </DataGrid>

模型视图:

namespace Admin.ViewModels
{
    class IncidentViewModel:BaseViewModel
    {
        private ObservableCollection<Incident> _incidents;
        public ObservableCollection<Incident> Incidents
        {
            get { return _incidents; }
            set 
            {
                _incidents = value;
                OnPropertyChanged("Incidents");
            }
        }

        public IncidentViewModel()
        {
            CC_RK2Entities context = new CC_RK2Entities();
            Incidents = new ObservableCollection<Incident>(context.Incident);
        }
    }
}

0 个答案:

没有答案