c# - how to stop a specific column to be generated in DataGrid when the AutoGenerateColumns is set to True? -


i have bound observablecollection datagrid , set autogeneratecolumns true in wpf mvvm application.

then how can stop specific column appeared in datagrid?

i have seen same question here. i'm looking more mvvm approach.

mvvm means ui , data layers separate, view layer merely being visual reflection of data layer.

so "mvvm way" of excluding column depends on exclusion should occur:

  • if data layer supposed exclude column, remove column data layer.

    this typically means modifying collection datagrid.itemssource binding no longer includes data should not visible.

    or depending on application , counts "application logic", may mean maintaining string or list<string> of columns exclude, , have view find way of binding strings , modifying it's display exclude columns (a custom dependency property, converter, re-use of tag property + autogeneratingcolumn event, triggers, etc)

  • if view layer supposed exclude column, remove column view layer.

    this done setting autogeneratecolumns="false" , specifying <datagrid.columns> yourself, can exclude column view layer using autogeneratingcolumn event of datagrid, , cancelling generation of column if it's columnname equals value want exclude, question linked suggested.

    private void datagrid_autogeneratingcolumn(     object sender, datagridautogeneratingcolumneventargs e) {     if ((string)e.column.header == "id")     {         e.cancel = true;     } } 

remember, whole point of mvvm separate ui , data layers. there's absolutely nothing wrong using code-behind view in mvvm, providing code related ui-specific logic only, , not data/application-specific logic


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -