private void RemoveGroup(Group gItem)
 {
     try
     {
         _cache.RemoveGroup(gItem);
         if (SelectedGroup == gItem)
         {
             SelectedGroup = ListGroups.FirstOrDefault();
         }
     }
     catch (WebException e)
     {
         ShowPopupWebException(e);
     }
 }
        public ManageGroupsViewModel(ISourcesCacheProvider cacheProvider, IDockWindow window)
        {
            _cache  = cacheProvider.CurrentCache;
            _window = window;

            Title = "Manage Groups";

            _cache.Groups
            .Connect()
            .Sort(SortExpressionComparer <Group>
                  .Ascending(x => x.ID))
            .ObserveOnDispatcher()
            .Bind(out _listGroups)
            .Subscribe()
            .DisposeWith(Disposables);

            _cache.InstancesWithoutGroup
            .Connect()
            .Sort(SortExpressionComparer <Instance>
                  .Ascending(x => x.ID))
            .ObserveOnDispatcher()
            .Bind(out _listWithoutGroup)
            .Subscribe()
            .DisposeWith(Disposables);

            var dynamicInListFilter = this
                                      .WhenAnyValue(x => x.SelectedGroup)
                                      .Select(g => (Func <Group, bool>)(x => g != null && x.ID == g.ID));

            _cache.Groups
            .Connect()
            .Filter(dynamicInListFilter)
            .TransformMany(x => x.Instances, x => x.ID)
            .Sort(SortExpressionComparer <Instance>
                  .Ascending(x => x.ID))
            .ObserveOnDispatcher()
            .Bind(out _listInGroup)
            .Subscribe()
            .DisposeWith(Disposables);

            SelectedGroup = ListGroups.FirstOrDefault();

            RemoveGroupCommand = ReactiveCommand.Create <Group>(RemoveGroup);
            CreateGroupCommand = ReactiveCommand.CreateFromTask(CreateGroup);
        }