How to refresh a Telerik datagrid using UWP and MVVM Light

风流意气都作罢 提交于 2019-12-13 04:09:10

问题


Thanks for visiting.

I am having trouble refreshing a Telerik grid when the Refresh button is clicked. In other words, if the user changes the data and decides not to save it, clicking the refresh button should reload the grid with the original values.

Here is my XAML in the View for the Refresh:

<Button Content="Refresh" Grid.Row="1" Margin="92,5,0,11" Command="{x:Bind ViewModel.RefreshDataCommand}"/>

Here is my XAML in the View for the Grid:

<tg:RadDataGrid ColumnDataOperationsMode="Flyout"  x:Name="grid" ItemsSource="{Binding Source,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{x:Bind ViewModel.CurrentUserData, Mode=TwoWay}" UserEditMode="Inline"  Grid.ColumnSpan="4" Grid.Row="1" AutoGenerateColumns="False">

Notice that in the above code the grid is bound to the Source property in the ViewModel. Here is the bound property in the ViewModel:

public ObservableCollection<UserData> Source
    {
        get
        {
            try
            {
                udCol = GetUserData(FileName).Result;
                return udCol;
            }
            catch (AggregateException ex)
            {

                return null;
            }

        }
        set
        {
            udCol = value;
        }
    }

The above property automatically loads the data in the grid when the page is navigated to.

Here is the function that the Refresh button is bound to:

private void RefreshDataCommandAction()
    {
        udCol[0].Name = "test1";
        CurrentUserData = udCol[0];
        Source = udCol;
        RaisePropertyChanged("Source");

    }

I was experimenting in the above function, that is why the code looks redundant but no matter what I do the new assignment in this function does not update the UI. Ideally, the user will update the cell in the UI and click Refresh to go back to original or just reload the data. The ViewModel inherits ViewModelBase which contains INotifyPropertyChanged and I thought that that should be enough to propagate the changes to the UI when the property is changed. I don't want to break the MVVM pattern just to update the UI.

I would really appreciate some help. Thanks a lot in advance!!!

EDIT:

I changed my XAML in the View back to this because it broke my ADD functionality:

<tg:RadDataGrid ColumnDataOperationsMode="Flyout"  x:Name="grid" ItemsSource="{x:Bind ViewModel.Source}" SelectedItem="{x:Bind ViewModel.CurrentUserData, Mode=TwoWay}" UserEditMode="Inline"  Grid.ColumnSpan="4" Grid.Row="1" AutoGenerateColumns="False">

回答1:


Source does not implement Change Notification, so the UI has no way of knowing you assigned a different instance.

When binding any form of list in MVVM, 3 parts need change notification:

  • the propery you expose the list on (source)
  • the list itself (observable collection takes care of that)
  • each property of the type you are exposing (every property of UserData)


来源:https://stackoverflow.com/questions/47984570/how-to-refresh-a-telerik-datagrid-using-uwp-and-mvvm-light

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!