wpf 如何使用 MVVM 刷新我的 DataGrid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31205944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to refresh my DataGrid using MVVM
提问by Joby James
I want to refresh my data grid when button click (MVVM Model). In my view model there is no reference about view. Can anybody explain.
我想在单击按钮时刷新我的数据网格(MVVM 模型)。在我的视图模型中,没有关于视图的参考。谁能解释一下。
i want to use DataGrid.Refresh() method when button click. How can i use this in MVVM Model.
我想在单击按钮时使用 DataGrid.Refresh() 方法。我如何在 MVVM 模型中使用它。
回答by Gul Md Ershad
Set your data item to ObservableCollectionand bind itemSourceof dataGridwith ObservableCollection.
Datagridwill be refreshed if entries will be added/removed or moved to this collection.
您的数据项设置为ObservableCollection和绑定itemSource的dataGrid使用ObservableCollection。
Datagrid如果将条目添加/删除或移动到此集合,将刷新。
回答by Liero
you need to databind your DataGrid to some collection in viewmodel:
您需要将您的 DataGrid 数据绑定到视图模型中的某个集合:
<DataGrid ItemsSource="{Binding Items}">...</DataGrid>
then, if your Itemsproperty is of type ObservableCollection, DataGrid is refreshed automatically, when items are added or removed from Items collection. There is no need to call DataGrid.Refresh()- this is why MVVM makes things simpler.
然后,如果您的Items属性是 类型ObservableCollection,则在 Items 集合中添加或删除项目时,DataGrid 会自动刷新。无需调用DataGrid.Refresh()- 这就是 MVVM 使事情变得更简单的原因。
public class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel()
{
Items = new ObservableCollection<SomeClass>();
//add some test data
Items.Add(new SomeClass());
Items.Add(new SomeClass());
RefreshCommand = new DelegateCommand(Refresh);
}
public DelegateCommand RefreshCommand { get private set; }
public ObservableCollection<SomeClass> Items { get; private set; }
private void Refresh()
{
Items.Clear();
//add actual items
Items.Add(new SomeClass());
Items.Add(new SomeClass());
}
}
Alternatively, you could cust create new instance of Items collection:
或者,您可以创建 Items 集合的新实例:
private void Refresh()
{
//in this case, items doesn't have to be ObservableCollection, but any collection type
Items = new ObservableCollection<SomeClass> {
new SomeClass(),
new SomeClass()
};
OnPropertyChanged("Items");
}
if you really need to access UIElement, then do it from codebehind, when something happens in viewmodel (use viewmodel event to notify view, that something has happened). In following sample I have used PropertyChanged event to notify view, that something in viewmodel has changed and view takes care of refreshing viewmodel.
如果你真的需要访问 UIElement,那么当视图模型中发生某些事情时,从代码隐藏中执行它(使用视图模型事件通知视图,发生了某些事情)。在以下示例中,我使用 PropertyChanged 事件通知视图,视图模型中的某些内容已更改,视图负责刷新视图模型。
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
((MainWindowViewModel) DataContext).PropertyChanged += ViewModel_PropertyChanged;
}
void ViewModel_PropertyChanged(object s, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Items")
{
MyDataGrid.Refresh();
}
}
回答by joe_maya
I am using DataTable bound to DataGrid. DataGrid wouldn't update UI if I added column to DataTable. To force it to do through ViewModel, I set the DataTable Property to null first(save it temporarily) and then set it back to the original DataTable. This worked for me.
我正在使用绑定到 DataGrid 的 DataTable。如果我将列添加到 DataTable,DataGrid 不会更新 UI。为了强制它通过 ViewModel 执行,我首先将 DataTable 属性设置为 null(临时保存),然后将其设置回原始 DataTable。这对我有用。

