Datagrid拒绝刷新并显示其数据,其中数据在WPF MVVM中的数据类型 [英] Datagrid refuse to refresh and show its data, where data is of type datatable in WPF MVVM

查看:212
本文介绍了Datagrid拒绝刷新并显示其数据,其中数据在WPF MVVM中的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//我已经解决了这个问题一个多星期了,现在我觉得我需要帮助。

//我的目标:将DataTable绑定到MVVM中的DataGrid。 br />
//以下是问题:



//在MainWindow.xaml中,代码是:

//Hi, I've been working on this problem for more than a week and now I think I need help here.
//My goal: bind DataTable to DataGrid in MVVM.
//Here is the question:

//in MainWindow.xaml, the code is :

<Grid>
  <DataGrid x:Name="dg" ItemsSource = "{Binding dtSrc}"/>
  <Button x:Name="loaddata" Command="{Binding File_OpenCommand}"/>
</Grid>




//in MainWindow.xaml.cs
public partial class MainWindow : Window
{
  public MainWindow()
  {
     this.DataContex = new MainWindowViewModel;
  }
}

//in MainWindowViewModel.cs
public class MainWindowViewModel
{
  public MainWindowViewModel()
  {
    File_OpenCommand = new DelegateCommand(new Action<object>(File_Open));
  }
  
  public DataTable dtSrc {get;set;}
  public DelegateCommand File_OpenCommand;
  public void File_Open(object obj)
  {
      // here is the problem:
      //dtSrc has data, but DataGrid refuse to refresh and show them.
      dtSrc = LoadData();
  }
}

//in DelegateCommand.cs
public class DelegateCommand : ICommand
{
    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    public DelegateCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
}





我尝试了什么:



我是WPF的新手,谷歌搜索了许多演示和样本,我仍然被困在这里。



What I have tried:

I'm a rookie in WPF and Googled many demos and samples, still, I'm stuck here.

推荐答案

DataTable 类早于WPF,专为WinForms应用程序设计。 WPF& WinForms使用两个非常不同的Binding系统。



为了证明这一点,如果你将 List 类绑定到WPF ItemsContainer 控件与< codeDataGrid一样,对集合的任何更改都不会被反映出来。这是因为未实现< code> INotifyCollectionChanged和 INotifyPropertyChanged 。当您查看 DataTable [ ^ ] class:
The DataTable class pre-dates WPF and was designed for WinForms applications. WPF & WinForms use two very different Binding systems.

To demonstrate this, if you bind a List class to a WPF ItemsContainer control like <codeDataGrid, any changes to the collection will not be reflected. This is due to the <code>INotifyCollectionChanged and INotifyPropertyChanged not being implemented. You can see this when you look at the DataTable[^] class:
[SerializableAttribute]
public class DataTable : MarshalByValueComponent, IListSource, 
	ISupportInitializeNotification, ISupportInitialize, ISerializable, 
	IXmlSerializable

WPF具有称为一个特殊的集合类的ObservableCollection< T> 。查看 ObservableCollection< t> [ ^ ]上课你可以看到两个实现了绑定接口:

WPF has a special collection class called ObservableCollection<T>. Looking at ObservableCollection<t>[^] class you can see the two interfaces for binding are implemented:

[SerializableAttribute]
public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, 
	INotifyPropertyChanged

您可能能够EXTEN的数据表类捕获键事件和反映行更改,但每个单元格也将需要实现 INotifyPropertyChanged 接口。这将是远simplier修改代码一起工作的的ObservableCollection< T> 和数据模型类(具有 INotifyPropertyChanged的已经实现了。



如果你想进一步了解WPF绑定系统是如何工作的,Pete O'Hanlon目前正在做一个很棒的多部分系列: Bare Metal MVVM - 代码遇见道路的地方 - 第1部分 [ ^ ]

You might be able to exten the DataTable class to capture key events and reflect the row changes, however each cell will also require the INotifyPropertyChanged interface implemented as well. It would be far simplier to modify your code to work with the ObservableCollection<T> and data model classes (with INotifyPropertyChanged implemented).

If you want to learn more about how the WPF binding system works, Pete O'Hanlon has a great multipart series that he is doing at the moment: Bare Metal MVVM - Where The Code Meets The Road - Part 1[^]


这篇关于Datagrid拒绝刷新并显示其数据,其中数据在WPF MVVM中的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆