开发者

Update ListView when properties of items change

开发者 https://www.devze.com 2023-01-18 13:48 出处:网络
I want to implement a panel to set user permissions. So i have a PermissionListView where the ItemSource is an ObservableCollection and in this PermissionListView I have a Checkbox for each Item whic

I want to implement a panel to set user permissions. So i have a PermissionListView where the ItemSource is an ObservableCollection and in this PermissionListView I have a Checkbox for each Item which is bound to PermissionViewModel.Checked. I debugged it and this works.

The user gets selected in another ListView(UserListView). But I dont see a more MVVM like method to check the Permissions which are contained in UserListView.SelectedItem.Permissions, than simply use the OnSelectionChanged event to iterate over the ObservableCollection and set PermissionV开发者_开发百科iewModel.Checked = true or false.

But since a change of the properties doesn't fire the CollectionChanged event the panel still shows the selection of the last user.

  1. Is there any more MVVM like way to accomplish a 'Check all PermissionViewModels that are also in UserListView.SelectedItem.Permissions' ?

  2. If not, how can i cause a visible update in the Listview after setting the Checked properties of the list's items?


I could use an example with actual users and permissions, so I'm going to go ahead and create one - please tell me if this doesn't match what you're trying to do.

You have users and permissions like this (I'm using * for selected and Yes or - for checked):

UserListView        PermissionsListView

*Fred*              Read Yes
Wilma               Write -
Barney              Execute -
Betty

So the PermissionsListView is currently showing Fred's permissions. It seems that you want to know how to change the list of permissions to show Barney's permissions when Barney is selected.

What you could do is bind the OnSelectionChanged to the model so that it updates the selected user, then provide the Permissions through another property.

MyPermissionsListModel : INotifyPropertyChanged
{
   Command OnSelectionChanged(User user) { 
       // Command which calls ChangeSelectedUser
   }

   private void ChangeSelectedUser(User user) {
       _user = user;
       PropertyChanged(this, new PropertyChangedEventArgs("Permissions");
   }

   public ObservableCollection<Permission> Permissions {
       return new ObservableCollection<Permission>(_user.Permissions);
   }
}

Then you use a data template or a grid view to show the checked or unchecked permissions. I presume whether they're checked or unchecked is part of the permissions here, otherwise wrap the permissions and checked / unchecked state in a little ViewModel of their own. If they're not on the user then go get them from wherever they're from.

Please feel free to ask anything I haven't understood; it's hard to work out exactly what you're doing without a picture!


As the other answer you should implement INotifyPropertyChanged - I would also introduce a CollectionView and set IsSynchronizedWithCurrentItem=true and handle selection changed on that instead separated from the view.

You could also check out http://mvvmfoundation.codeplex.com/ and the PropertyObserver class.

0

精彩评论

暂无评论...
验证码 换一张
取 消