Toggle distinct and actual items in list

时间:2015-07-28 16:07:26

标签: c# .net wpf mvvm wpf-4.0

FileRecord is the observable collection that is being binded with my wpf datagrid in MVVM model.

I have one checkbox for each column above my datagrid. Checkbox name is "SelectUnique--Columnname--". When I click those checkboxes it should show unique values for the column in my grid.

When I click unique check box for TId, I do below logic

 var grpd = FileRecord.GroupBy(item => item.TID).Select(grp => grp.First());
 FileRecord= new ObservableCollection<FileData>(grpd); // will refresh the grid.

Then again When I click unique check box for CId, I do below logic

  var grpd = FileRecord.GroupBy(item => item.CID).Select(grp => grp.First());
  FileRecord= new ObservableCollection<FileData>(grpd);// will refresh the grid.

and so on. In this case, for example, if I do unique selection for all my columns, then again If I want to deselect the checkbox randomly(not in the order I selected unique checkboxes) I would like to undo what I have done for that particular column. For example, if I unselect CID unique check box, then the grid should so proper result.

How to acheive this? Please help.

2 个答案:

答案 0 :(得分:1)

When I want to filter a collection like this I have a property like this:

public IEnumerable<FileData> FilteredFiles
{
    get
    {
        if (Unique)
        {
            return Files.GroupBy(item => item.TID).Select(grp => grp.First());
        }
        else
        {
            return Files.GroupBy(item => item.CID).Select(grp => grp.First());
        }
    }
}

public ObservableCollection<FileData> Files
{
    get; set;
}

public bool Unique
{
    get
    {
        return unique;
    }
    set
    {
        unique = value;
        RaisePropertyChanged("FilteredFiles");
    }
}

Bind to FilteredFiles and when you add/remove from the collection just call RaisePropertyChanged("FilteredFiles") to notify the UI.

答案 1 :(得分:0)

You should have a reference of the original collection somewhere, and do all calculations over that one.

For instance, you could have a single method that gets called whenever a CheckBox is checked or unchecked, and have that method filter/group the original collection.

// Simplified properties
private IEnumerable<FileData> FileRecordCollection;

public ObservableCollection<FileData> FileRecord { get; set; }

// Event handlers for the CheckBoxes
private void TID_CheckBox_Checked(object sender, RoutedEventArgs e)
{
    UpdateFileRecord();
}

private void TID_CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    UpdateFileRecord();
}

// etc.

// Method that updates FileRecord
private void UpdateFileRecord()
{
    IEnumerable<FileData> groupedCollection = FileRecordCollection;

    if (TID_CheckBox.IsChecked)
        groupedCollection = groupedCollection.GroupBy(item => item.TID).Select(grp => grp.First());

    if (CID_CheckBox.IsChecked)
        groupedCollection = groupedCollection.GroupBy(item => item.CID).Select(grp => grp.First());

    // etc.

    FileRecord = new ObservableCollection<FileData>(groupedCollection);
}

This isn't exactly optimal, but I can't think of something better (performance-wise) right now.