Reputation: 429
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.
i want to use DataGrid.Refresh() method when button click. How can i use this in MVVM Model.
Upvotes: 0
Views: 10265
Reputation: 35
I know its a little late for this, but this is my workaround to refresh DataGrid in wpf mvvm:
private void RefreshDataGrid()
{
InvoiceModel temp;
temp = Invoice;
Invoice = null;
Invoice = temp;
}
This is XAML:
<DataGrid ItemsSource="{Binding Invoice.Items}" SelectedItem="{Binding SelectedItem}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False">
As you see its just a reassigning Invoice
which provides ItemSource
for DataGrid and it easily refreshes the DataGrid.
Upvotes: 0
Reputation: 195
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.
Upvotes: 0
Reputation: 27328
you need to databind your DataGrid to some collection in viewmodel:
<DataGrid ItemsSource="{Binding Items}">...</DataGrid>
then, if your Items
property 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.
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:
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.
/// <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();
}
}
Upvotes: 1
Reputation: 1771
Set your data item to ObservableCollection
and bind itemSource
of dataGrid
with ObservableCollection
.
Datagrid
will be refreshed if entries will be added/removed or moved to this collection.
Upvotes: 1