Sunday, September 12, 2010

WPF DataGrid and grouping issues

In software applications there are numerous places where we use WPF Datagrid and applying grouping which is dependent on presenter logic. 

Ideally as per MVP architecture what we do is that we change the source (presenter) property with another record which is bound to target’s (view / in this case mostly DataGrid) ItemsSource. Now changing the ItemsSource member of the datagrid with another value or null does not update its associated grouping and group descriptions which in turns gives an exception that “The specified Visual is not an ancestor of this Visual”. 


One of the workaround for this is that whenever we change the presenter bound ItemsSource we should update the grouping as well.

3 comments:

  1. We update the grouping on the view by raising an event from the presenter.

    >> Presenter,
    OnListUpdated(Collection inNewValues) {
    Values = inNewValue;
    RaiseEvent();
    }

    >> View,
    OnEventReceived() {
    // remove the previous grouping applied to the datagrid
    // check if new grouping has to be applied to the datagrid, if so then add
    }

    ReplyDelete