0
votes

I am using MVVM pattern. I want to use datagrid's refresh method . I have also gone through following link and its useful: How to refresh a WPF DataGrid?

but my problem is : I have created one datagrid object in viewmodel like this:

    public DataGrid ActiveGrid=new DataGrid();
public void RefreshGrid()
        {
           View.Dispatcher.Invoke((Action)(() =>
                ActiveGrid.Items.Refresh()

                ));
        }

and my datagrid's itemsource is already bound to another collection e.g

<DataGrid x:Name="ActiveGrid"
                      IsReadOnly="True"                      
                      ItemsSource="{Binding ActiveCallCollection}"
                       SelectedItem="{Binding ActiveGridCollection,
                                             Mode=TwoWay}" SelectionMode="Single"
                      Visibility="{Binding IsActiveCallsSelected,
                                           Converter={StaticResource BooleanToVisibilityConverter}}"
                      d:LayoutOverrides="Height">

How can I bind ActiveGrid to xaml datagrid.

2
I'm not sure why are you creating this in your viewmodel. If you want to update the datagrid because youa added items to ActiveCallCollection you need to use the INotifyPropertyChanged to tell the UI something has changed in that collection. If you are indeed databinding a collection and adding items dynamicly I suggest you use ObservableCollection. If you add an item to this collection it will notify itself already that it has obtained new items.Rik van den Berg
my ActiveCallCollection is actually an ObservableCollection. i have also tried with INotifyPropertyChanged. but datagrid gets updated when I toggle the rows, its not working as soon as data comes in ObservableCollection. Or can I use refresh() method directly in my xaml Window. and how to?deathrace
well to answer your question "How can I bind ActiveGrid to xaml datagrid." You can remove the datagrid from xaml make it a ContentPresenter and make the ActiveGrid a public Property. <ContentPresenter Content="{Binding ActiveGrid}" > but I think thats not the correct option here. If you are created a NEW observable collection after the constructor you do need to NotifyPropertyChanged the collection.Rik van den Berg
my requirement is there must be datagrid. so is there any option to bind my ActiveGrid object to datagrid other than using ItemsSourcedeathrace
The DataGrid is not an appropriate control for displaying live updating data. What happens if a user is entering data in a field and it gets updated in the background to blow away the user's edits?Greg D

2 Answers

0
votes

if you have a DataGrid object in your ViewModel then its not MVVM anymore. you better ask what you want to achieve, so we can help you.

if you bind to a OberservableCollection - you should to this once! all changes(add, remove) are then reflected in your datagrid.

pls post your code where you create/alter your collection

0
votes

If your using the MVVM design pattern, your view model should not have a DataGrid

Instead your View should have a DataGrid, and it should be bound to an ObservableCollection in the ViewModel.

Be sure you bind the ItemsSource property and not set it, or the changes to the ObservableCollection may not automatically update in the UI

<DataGrid x:Name="ActiveGrid"
          IsReadOnly="True"                      
          ItemsSource="{Binding ActiveCallCollection}"
          SelectedItem="{Binding Selected ActiveCall}" 
          SelectionMode="Single"
          Visibility="{Binding IsActiveCallsSelected,
                               Converter={StaticResource BooleanToVisibilityConverter}}"
          d:LayoutOverrides="Height">